Reply
Old 05-27-2008, 05:33 PM br2nl ??
Gilligan's Avatar
Dead Like Me

Posts: 1,608
Name: Stefan
Location: London, UK
I know how to use nl2br, but how do I work backwards (change <br /> into new lines) ??
Gilligan is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 05-27-2008, 06:03 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
PHP Code:
$br2nl_string str_replace('<br />',"\n",$original_string); 
If you want a bit more flexibility on whether it's XHTML or HTML, this should work, but takes a bit more work for PHP


PHP Code:
$br2nl_string preg_replace('/<br ?\/?>/',"\n",$original_string); 
I didn't test that regex, but ? means 0 or 1, so it should work.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites

Last edited by JeremyMiller : 05-27-2008 at 06:13 PM. Reason: Forgot the newline!
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 06:10 PM Re: br2nl ??
Plugin-Developer's Avatar
Weightlifting CS Student

Posts: 505
Name: Nick Ohrn
If he wanted new lines in the new string, shouldn't he use the following?

PHP Code:
$br2nl_string str_replace'<br />'"\n"$original_string ); 
__________________
Plugin-Developer.com - Custom plugin development to fit your needs. Plugins available for WordPress and Drupal, among others.
Plugin-Developer is offline
Reply With Quote
View Public Profile Visit Plugin-Developer's homepage!
 
Old 05-27-2008, 06:13 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Quote:
Originally Posted by nickohrn View Post
If he wanted new lines in the new string, shouldn't he use the following?

PHP Code:
$br2nl_string str_replace'<br />'"\n"$original_string ); 

Doh! Of course! lol. Thanks. TK coming your way.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 06:17 PM Re: br2nl ??
Gilligan's Avatar
Dead Like Me

Posts: 1,608
Name: Stefan
Location: London, UK
its just displaying <br /> still
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-27-2008, 06:22 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Can you give a code snippet? Since you made that remark, I created a test script:

PHP Code:
<?php
$original_string 
'ABC DEF<br />GHI JKL MNOP<br />';
$br2nl_string str_replace'<br />'"\n"$original_string );

print 
'<pre>'.$br2nl_string.'</pre>';

$br2nl_string preg_replace('/<br ?\/?>/i',"\n",$original_string);
print 
'<pre>'.$br2nl_string.'</pre>';
?>
You'll notice I added an i in the preg_replace so that it can be <br /> or <Br />, etc.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 06:34 PM Re: br2nl ??
Gilligan's Avatar
Dead Like Me

Posts: 1,608
Name: Stefan
Location: London, UK
So far there's nothing really in the script
so here's the whole thing

PHP Code:
<?php 



$index 
fopen("index.html""r+");

$index2 fread($indexfilesize("index.html"));

$heading '<!-- main heading -->';
$headinge "<!-- end main heading -->";
    list(, 
$data_split) = explode($heading$index22);
    list(
$ext2, ) = explode($headinge$data_split2);
    
echo 
'Heading: <input type="text" value="'.$ext2.'">';    
    
$para1 '<!-- Start first -->';
$para1e "<!-- End first -->";
    list(, 
$data_split) = explode($para1$index22);
    list(
$ext, ) = explode($para1e$data_split2);
    
$extrep preg_replace('/<br ?\/?>/',"\n"$ext);
echo 
'<textarea cols="60" rows="10">'.$extrep.'</textarea>';

fclose($index);



?>
So whats happening here is:

The script opens the file 'index.html', and the part wrapped in comments saying 'start first' and 'end first' are displayed in a textarea. Whats meant to happen is any <br /> tag inside the textarea should be replaced with new lines
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-27-2008, 06:37 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
I used
HTML Code:
<html>
  <head>
  </head>
  <body>
    <!-- main heading -->
    This is the main heading
    <!-- end main heading -->
    <!-- Start first -->
    ABC DEF GHI
    <br />
    JKL MNO
    <br />PQR STU VWX
    <br />YZ
    <!-- End first -->
</body>
For the index.html and got
Code:
    ABC DEF GHI
    

    JKL MNO
    
PQR STU VWX
    
YZ
as the result. Seems to be working (though, I'd add in that i I was talking about for greater flexibility).
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 06:41 PM Re: br2nl ??
Gilligan's Avatar
Dead Like Me

Posts: 1,608
Name: Stefan
Location: London, UK
And you used my exact code?? strange..

EDIT:

Grrr.. I kept refreshing and nothing changed, so then i done a complete refresh (CTRL + F5 for firefox) and it worked.

Thanks

Last edited by Gilligan : 05-27-2008 at 06:43 PM.
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-27-2008, 06:43 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Yep. Just copy-and-pasted. What is the HTML you're using (if gigantic, maybe a short snippet would be good).
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 06:45 PM Re: br2nl ??
Gilligan's Avatar
Dead Like Me

Posts: 1,608
Name: Stefan
Location: London, UK
Just realized the problem (read edit of last post to see)
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 05-27-2008, 06:50 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Glad you caught it. Bit by the cache. We've all done that more than once! lol.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 06:56 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Just thought of a code optimization. I believe this will be faster, but you may want to do some benchmark testing:

PHP Code:
<?php
$index 
fopen("index.html""r+");

$index2 fread($indexfilesize("index.html"));

$heading '<!-- main heading -->';
$headinge "<!-- end main heading -->";
$start_position strpos($index2,$heading);
$end_position strpos($index2,$headinge,$start_position);

$ext2 substr($index2,$start_position+21,$end_position $start_position 21);

echo 
'Heading: <input type="text" value="'.$ext2.'">'.'<br />';

$para1 '<!-- Start first -->';
$para1e "<!-- End first -->";

$start_position strpos($index2,$para1);
$end_position strpos($index2,$para1e,$start_position);

$ext substr($index2,$start_position+20,$end_position $start_position 20);

$extrep preg_replace('/<br ?\/?>/i',"\n"$ext);
echo 
'<textarea cols="60" rows="10">'.$extrep.'</textarea>';

fclose($index);
?>
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites

Last edited by JeremyMiller : 05-27-2008 at 06:57 PM.
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 07:06 PM Re: br2nl ??
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Ok. I must be having too much fun with this. If you change your tokens to require them of the form <!-- start _____ --> and <!-- end ____ --> where the ____ are equal, then this code may be helpful and more generalized:

PHP Code:
<?php
$index 
fopen("index.html""r+");

$index2 fread($indexfilesize("index.html"));

$successful preg_match_all('/<!-- start ([a-z ]+?) -->(.+?)<!-- end (\1) -->/si',$index2,$matches);
//print '<pre>'.htmlentities(print_r($matches,true)).'</pre>';
if ((int)$successful 0) {
  foreach (
$matches[1] as $match_index=>$match_type) {
    switch (
$match_type) {
      case 
'main heading':
        echo 
'Heading: <input type="text" value="'.trim($matches[2][$match_index]).'">'.'<br />';
        break;
      case 
'first':
        
$output_value preg_replace('/<br ?\/?>/i',"\n"$matches[2][$match_index]);
        echo 
'<textarea cols="60" rows="10">'.trim($output_value).'</textarea>';
        break;
    }
  }
}
fclose($index);
?>
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-27-2008, 07:15 PM Re: br2nl ??
Gilligan's Avatar
Dead Like Me