By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Find and cut string

P: n/a
Hi,

I'm new to PHP, so please bear with me! =)

Is there a nice little function to find a string between to html-tags?
I.e. let's say I have a string with the following text:
"Hello world, <b>this is a test</b>"
I would like to cut out the text between the <b> and </b> ("this is a test").

Thanks in advance!
Best regards
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In article <5c*************************@posting.google.com> ,
ol*@ekedahl.nu (Ola Ekedahl) wrote:
Is there a nice little function to find a string between to html-tags?
I.e. let's say I have a string with the following text:
"Hello world, <b>this is a test</b>"
I would like to cut out the text between the <b> and </b> ("this is a test").


Here are two ways, one with preg_match and one with list and split:

<?
$string = "Hello world, <b>this is a test</b>";
preg_match('/<b>.*<\/b>/i', $string, $result);
echo strip_tags($result[0]);
?>

<?
$string = "Hello world, <b>this is a test</b>";
list($junk, $good) = split('<b>', $string);
list($good, $junk) = split('</b>', $good);
echo $good;
?>

Both methods would get more complicated if the HTML gets more
complicated.

hth

--
Bulworth : fu***@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.