 |
|
|
05-27-2008, 05:33 PM
|
br2nl ??
|
Posts: 1,670
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: 1,712
Name: Jeremy Miller
Location: Las Vegas, 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
Please login or register to view this content. Registration is FREE
Last edited by JeremyMiller; 05-27-2008 at 06:13 PM..
Reason: Forgot the newline!
|
|
|
|
05-27-2008, 06:10 PM
|
Re: br2nl ??
|
Posts: 504
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 );
__________________
Please login or register to view this content. Registration is FREE - Custom plugin development to fit your needs. Plugins available for WordPress and Drupal, among others.
|
|
|
|
05-27-2008, 06:13 PM
|
Re: br2nl ??
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, 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.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-27-2008, 06:17 PM
|
Re: br2nl ??
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
its just displaying <br /> still 
|
|
|
|
05-27-2008, 06:22 PM
|
Re: br2nl ??
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, 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
Please login or register to view this content. Registration is FREE
|
|
|
|
05-27-2008, 06:34 PM
|
Re: br2nl ??
|
Posts: 1,670
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: 1,712
Name: Jeremy Miller
Location: Las Vegas, 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
Please login or register to view this content. Registration is FREE
|
|
|
|
05-27-2008, 06:41 PM
|
Re: br2nl ??
|
Posts: 1,670
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: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Yep. Just copy-and-pasted. What is the HTML you're using (if gigantic, maybe a short snippet would be good).
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-27-2008, 06:45 PM
|
Re: br2nl ??
|
Posts: 1,670
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: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Glad you caught it. Bit by the cache. We've all done that more than once! lol.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-27-2008, 06:56 PM
|
Re: br2nl ??
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, 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); ?>
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
Last edited by JeremyMiller; 05-27-2008 at 06:57 PM..
|
|
|
|
05-27-2008, 07:06 PM
|
Re: br2nl ??
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, 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); ?>
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-27-2008, 07:15 PM
|
Re: br2nl ??
|
Posts: 1,670
Name: Stefan
Location: London, UK
|
woah woah woah, thats way beyond me
Could you slow down and explain what you done a little more please? 
|
|
|
|
05-27-2008, 07:22 PM
|
Re: br2nl ??
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
lol. All I did was make a single regular expression to check the full document for all code blocks of the form described, and loop through them. The part your replace the _____'s with is what you should use in the case statement.
So, let's see some code instead of just verbiage:
HTML Code:
<html>
<head>
</head>
<body>
<!-- start 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>
is what I used to test the script above. You'll notice that there are 2 codeblocks and that the underscores in my format I gave you have been replaced with main heading and first, respectively.
Let's extend this a bit and say that you want to go ahead and add a second "first" block:
HTML Code:
<html>
<head>
</head>
<body>
<!-- start 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 -->
<!-- Start first -->
abc def ghi
<br />
jkl mno
<br />pqr stu vwx
<br />yz
<!-- End first -->
</body>
That code will automatically process it. Now, let's say that you wanted to add a whole new type of section, say "navigation". You'd adjust the HTML like this:
HTML Code:
<html>
<head>
</head>
<body>
<!-- start 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 -->
<!-- Start navigation -->
<a href="index.php">Home</a>
<a href="login.php">Login</a>
<a href="faq.php">FAQ</a>
<!-- End navigation -->
</body>
and then adjust the PHP to take care of another "case" like this (I added a couple of titles and line breaks for readability):
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 'Body: <br /><textarea cols="60" rows="10">'.trim($output_value).'</textarea><br />'; break; case 'navigation': echo 'Navigation: <br /><textarea cols="60" rows="10">'.trim($matches[2][$match_index]).'</textarea><br />'; break; } } } fclose($index); ?>
You can extend it as much as you please and handle as many tags as you want.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
|
« Reply to br2nl ??
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|