preg_match_all whitespace question

Discussion in 'Programming' started by Silver89, Aug 12, 2009.

  1. #1
    Hi,

    I'm trying to find the Link Title within the following:

    
    <li class="cuisine">
    									Cuisine 	
    							 		
                                   					<a title="New Zealand food" href="/search.php?">
                                						New Zealand
                           			 			</a>
    Code (markup):
    I'm using the following php:

    
    preg_match_all('~<li class="cuisine">
    									Cuisine 	
    							 		
                                   					<a title="(.*?)"~s', $html, $cuisineMatches);
    			foreach ($cuisineMatches[1] AS $cuisineMatch)
    			{ 
    				//echo "Cuisine: ".str_replace(" recipes", "", $cuisineMatch)."<br />";	
    			}
    
    PHP:
    But in some instances the white space changes so it can't find the match, what can I do about this?

    Thanks
     
    Silver89, Aug 12, 2009 IP
  2. alons

    alons Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use this instead:
    
    preg_match_all('~<li class="cuisine">(\s*?)Cuisine(\s*?)<a title="(.*?)"~is', $html, $cuisineMatches);
    foreach ($cuisineMatches[1] AS $cuisineMatch)            {                 //echo "Cuisine: ".str_replace(" recipes", "", $cuisineMatch)."<br />";             }
    
    PHP:
     
    alons, Aug 13, 2009 IP