 |
05-27-2008, 05:33 PM
|
br2nl ??
|
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) ??
|
|
|
|
05-27-2008, 06:03 PM
|
Re: br2nl ??
|
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.
Last edited by JeremyMiller : 05-27-2008 at 06:13 PM.
Reason: Forgot the newline!
|
|
|
|
05-27-2008, 06:10 PM
|
Re: br2nl ??
|
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.
|
|
|
|
05-27-2008, 06:13 PM
|
Re: br2nl ??
|
Posts: 984
Name: Jeremy Miller
Location: Reno, NV
|
Quote:
Originally Posted by nickohrn
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.
|
|
|
|
05-27-2008, 06:17 PM
|
Re: br2nl ??
|
Posts: 1,608
Name: Stefan
Location: London, UK
|
its just displaying <br /> still 
|
|
|
|
05-27-2008, 06:22 PM
|
Re: br2nl ??
|
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.
|
|
|
|
05-27-2008, 06:34 PM
|
Re: br2nl ??
|
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($index, filesize("index.html"));
$heading = '<!-- main heading -->'; $headinge = "<!-- end main heading -->"; list(, $data_split) = explode($heading, $index2, 2); list($ext2, ) = explode($headinge, $data_split, 2); echo 'Heading: <input type="text" value="'.$ext2.'">'; $para1 = '<!-- Start first -->'; $para1e = "<!-- End first -->"; list(, $data_split) = explode($para1, $index2, 2); list($ext, ) = explode($para1e, $data_split, 2); $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
|
|
|
|
05-27-2008, 06:37 PM
|
Re: br2nl ??
|
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).
|
|
|
|
05-27-2008, 06:41 PM
|
Re: br2nl ??
|
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.
|
|
|
|
05-27-2008, 06:43 PM
|
Re: br2nl ??
|
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).
|
|
|
|
05-27-2008, 06:45 PM
|
Re: br2nl ??
|
Posts: 1,608
Name: Stefan
Location: London, UK
|
Just realized the problem (read edit of last post to see)
|
|
|
|
05-27-2008, 06:50 PM
|
Re: br2nl ??
|
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.
|
|
|
|
05-27-2008, 06:56 PM
|
Re: br2nl ??
|
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($index, filesize("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); ?>
Last edited by JeremyMiller : 05-27-2008 at 06:57 PM.
|
|
|
|
05-27-2008, 07:06 PM
|
Re: br2nl ??
|
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($index, filesize("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); ?>
|
|
|
|
05-27-2008, 07:15 PM
|
Re: br2nl ??
|
|
| |