Clan Adverts

www.afgserv.com

 

Return just part of a string in php

Description: Very useful in most occasions
Version: 1.0
Added on: 24 February 2007
Author: unkown
Difficulty Level: Very Easy
Views: 346
Rating: 10.0 (1 Vote)
Detailed Profile

Basically, we have a long string:

Quote:

bla bla
bla bla
I want this info
bla bla

bla bla



We want to cut out the part that says 'I want this info'.
Simply use the eregi function like below:

Code:

$string = '<aa>bla bla</aa><br><bb>bla bla</bb><br><cc>I want this info</cc><br><dd>bla bla</dd><br><ee>bla bla</ee><br>';
$string = eregi('<cc>(.*)</cc>', $string, $data);
echo $data[1];


The important part of this tutorial is the
(.*) code that lies between:
Code:

'<cc>(.*)</cc>'


What that does is simply grab everything in between and tags.

Pretty simple? More info can be found at www.php.net/eregi