RE: [PHP] Showing only part of string
| From: | Dave Carrera | Date: | Fri, 07 May 2004 12:09:28 +0000 |
| Subject: | RE: [PHP] Showing only part of string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-185605@lists.php.net to get a copy of this message | ||
Thanks John,
Works a treat.
Thank You
Dave Carrera
-----Original Message-----
From: John W. Holmes [mailto:holmes072000@charter.net]
Sent: 07 May 2004 12:32
To: Dave Carrera
Cc: php-general@lists.php.net
Subject: Re: [PHP] Showing only part of string
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
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.677 / Virus Database: 439 - Release Date: 04/05/2004
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.677 / Virus Database: 439 - Release Date: 04/05/2004