how to fetch string using php function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how to fetch string using php function

Post by itsmani1 »

i want to select ABC and DEF using some php method/funciton? is there any?

Code: Select all

<OPTION>ABC</OPTION> 

<OPTION>DEF</OPTION>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

something like

Code: Select all

<?
// the string
$string = "<OPTION>ABC</OPTION>";

// store matches in array
$match = array();

// search for a match
preg_match('~<OPTION>(.*?)</OPTION>~si', $string, $match);

echo $match[0];

// outputs: ABC
?>
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

$string = "<OPTION>ABC</OPTION><OPTION>DEF</OPTION>";

// store matches in array
$match = array();

// search for a match
preg_match('~<OPTION>(.*?)</OPTION>~si', $string, $match);

echo $match[0];
echo $match[1];
but in this case it don't gave me
ABC
DEF
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Use preg_match_all instead

Code: Select all

<?

// the string
$string = "<OPTION>ABC</OPTION><OPTION>DEF</OPTION>";

// store matches in array
$match = array();

// search for a match
preg_match_all('~<OPTION>(.*?)</OPTION>~', $string, $match);

//echo $match[0];

echo "<pre>";
print_r($match);
echo "</pre>";

?>
Post Reply