note 84413 added to function.preg-split
| From: | harrismw at ids dot org dot au | Date: | Sun, 13 Jul 2008 13:33:19 +0000 |
| Subject: | note 84413 added to function.preg-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141448@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 194.153.94.10
Probable Submitter: 212.152.25.57
----
Manual Page -- http://www.php.net/manual/en/function.preg-split.php
Edit -- https://master.php.net/note/edit/84413
Del: integrated -- https://master.php.net/note/delete/84413/integrated
Del: useless -- https://master.php.net/note/delete/84413/useless
Del: bad code -- https://master.php.net/note/delete/84413/bad+code
Del: spam -- https://master.php.net/note/delete/84413/spam
Del: non-english -- https://master.php.net/note/delete/84413/non-english
Del: in docs -- https://master.php.net/note/delete/84413/in+docs
Del: other reasons-- https://master.php.net/note/delete/84413
Reject -- https://master.php.net/note/reject/84413
Search -- https://master.php.net/manage/user-notes.php