Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace() 2

Status
Not open for further replies.

youradds

Programmer
Joined
Jun 27, 2001
Messages
817
Location
GB
Hi. I have the following line;

<option value=&quot;Aberdeenshire&quot;>Aberdeenshire</option>

I have over 200 of these held in an array, and am using a 'foreach' to grab the stuff inside the <option>*</option> tag. I currently have;

$line = preg_replace(&quot;/<option value=\&quot;(.+?)\&quot;>/&quot;,&quot;&quot;,$line);
$line = preg_replace(&quot;/<\/option>/&quot;,&quot;&quot;,$line);

However, this is not working for me. It simply returns things like;

UL

Anyone got any ideas where I'm going wrong? In Perl it would be easy;

$var =~ m/<option value=\&quot;(.+?)\&quot;>(.+?)</option>/ and $val = $2;

I'm quite new to PHP....so any help would be much appreciated :)

Cheers

Andy

 
$line = preg_replace(&quot;<option value=\&quot;(.+?)\&quot;>&quot;,&quot;&quot;,$line);
$line = preg_replace(&quot;</option>&quot;,&quot;&quot;,$line);

does this work?


Known is handfull, Unknown is worldfull
 
Why don't you extract the content rather than stripping the delimiters?
Code:
preg_match('/<option.+>(.*)<\/option>/',$str,$array);
# or as above in one line
preg_replace('/(<option.+>)(.*)(<\/option>)/',&quot;$2&quot;,$subject);
Write a function stripOption and do an array callback&quot;
Code:
function stripOption($subject){
   return(preg_replace('/(<option.+>)(.*)(<\/option>)/',&quot;$2&quot;,$str));
}
$cleanArray = array_map(&quot;stripOption&quot;, $fullArray);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top