note 84413 deleted from function.preg-split by danbrown
| From: | danbrown@php.net | Date: | Wed, 27 May 2009 13:49:20 +0000 |
| Subject: | note 84413 deleted from function.preg-split by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156066@lists.php.net to get a copy of this message | ||
Note Submitter: harrismw at ids dot org dot au
----
If you have some text of the form:
$part1.'some text'.$part2.'some other text'.$part3
And you want to split out the text, and discard the $partX parts, e.g.
<html><head><title>
My title!
</title></head><body>
My body!
</body></html>
and just keep "My title!" and "My body!", then this code will be of use.
<?php
//defining the delimiters
$part1= '<html><head><title>';
$part2='</title></head><body>';
$part3='</body></html>';
//and the text, for my project I'm getting this information from a text file.
$tempcon = <<<eoa
<html><head><title>
My title!
</title></head><body>
My body!
</body></html>
eoa
;
// defining the patterns.
$pattern1='~'.$part1.'~';
$pattern2='~'.$part2.'~';
$pattern3='~'.$part3.'~';
//Finding the title
$arrayf = preg_split ($pattern1, $tempcon); //2 sort out the title
$title = $arrayf[1];
$arrayf = preg_split ($pattern2, $title); //2
$title = $arrayf[0];
//Finding the body text.
$arrayf = preg_split ($pattern2, $tempcon); //2 sort out the content
$content = $arrayf[1];
$arrayf = preg_split ($pattern3, $content); //2
$content = $arrayf[0];
//And display.
echo $title.'<br>'.$content;
?>
I looked long and hard, and I just couldn't find an example of doing this, so here you go. Of
course, I would love to be able to use a single call to preg_split, but I haven't worked that
out.