Re: Showing only part of string
| From: | John W. Holmes | Date: | Fri, 07 May 2004 11:31:39 +0000 |
| Subject: | Re: Showing only part of string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185602@lists.php.net to get a copy of this message | ||
Dave Carrera wrote:
$string = "This is a test string to <title>SHOW ONLY THIS BIT</title> of the string"; I have tried strpos, str_replace and other string manipulation things but I cant get my head around how to achieve showing the text with the <title></title> tags of the string. Any help is appreciated and I thank you in advance for any help given.
$b = strpos($str,'<title>')+7;
$l = strpos($str,'</title>') - $b;
$m = substr($str,$b,$l);
Sure, you can use regular expressions, but there's not really a need unless this gets more complex. Even though you're executing more code with this solution, it'll be faster than preg_match() (go ahead and benchmark it).
--
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/
php|architect: The Magazine for PHP Professionals – www.phparch.com