Author Topic: Do not match if link tags?  (Read 429 times)

0 Members and 1 Guest are viewing this topic.

Offline Andy WFUKTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Do not match if link tags?
« on: February 22, 2010, 10:08:33 AM »
Hi,

I am creating a function which converts certain car makes into links if they are found in the past string. The problem I have is some makes, such as Land Rover clash with Rover. So, to begin the function adds a link in for Land Rover, but then it also adds another link for Rover (i.e. it trys to add a link within the Land Rover link)! So I end up with messy code which doesn't work properly.

Here is the function:


$inputstring 
"Test text Land Rover, lotus are two very decent car brands as is a Porsche or Rover.";
$carmake['from'][0] = '/land rover\b/i';
$carmake['from'][1] = '/lotus\b/i';
$carmake['from'][2] = '/porsche\b/i';
$carmake['from'][3] = '/rover\b/i';
$carmake['to'][0] = '<a href="land_rover">Land Rover</a>';
$carmake['to'][1] = '<a href="lotus">Lotus</a>';
$carmake['to'][2] = '<a href="porsche">Porsche</a>';
$carmake['to'][3] = '<a href="rover">Rover</a>';

function 
car_make_to_link($inputstring$carmake$carmakereplace) {
	

	
$brandstr preg_replace($carmake$carmakereplace$inputstring);
	
return 
$brandstr;
}


Is there a way of changing my regex so it excludes matches which are within link tags or the href="" - i.e. within > and </ and also within href=" and "


This should stop my problem I think - or does anyone know of a different way to tackle the problem?

Many thanks
« Last Edit: February 22, 2010, 10:09:59 AM by Andy WFUK »

Offline sader

  • Enthusiast
  • Posts: 268
    • View Profile
Re: Do not match if link tags?
« Reply #1 on: February 22, 2010, 10:50:50 AM »
my suggestion for your task


function car_make_to_link($match)
{
  
$car_id str_replace(" ""_"strtolower($match[1])); //this will convert "Land Rover" to "land_rover" etc.
  
return "<a href='$car_id'>".$match[1]."</a>";
}

$str "Test text Land Rover, lotus are two very decent car brands as is a Porsche or Rover.";
echo 
preg_replace_callback('/(?<!>)(land rover|rover|porsche)(?!<)/i',"car_make_to_link"$str);

Offline Andy WFUKTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Do not match if link tags?
« Reply #2 on: February 22, 2010, 11:28:31 AM »
Thanks a lot!

BTW, I changed it a little bit:


echo preg_replace_callback('/(?!(?=[^<>]*>))(land rover|rover|porsche|lotus)(?!<)/i',"car_make_to_link"$str);


Does that seem okay to you? I am not great at REGEX, but I was running into trouble if there was already a HTML link in the string, such as:

$str "Test text Land Rover, Lotus are two very decent car brands as is a Porsche or Rover. <a href='blah/rover/' title='This Rover Is Great'>Rover</a>."

The change I made seems to make it so it ignores that link too  :D
Thanks again - I must really lean more REGEX!
« Last Edit: February 22, 2010, 11:29:03 AM by Andy WFUK »

Offline Andy WFUKTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Do not match if link tags?
« Reply #3 on: February 23, 2010, 11:55:48 AM »
I noticed a problem with the above solution. If the string has a link already in it, or it has
Code: [Select]
<br /> or
Code: [Select]
<p></p> it will not work, as the REGEX excludes strings within them too. Is there anyway of modifying the above function so it works when there is HTML already in the passed string?

The REGEX should be excluding the words if a) they are within a tags attributes e.g.
Code: [Select]
<a href="exclude_this_path" title="and exclude this" and b) when the text is within link tags e.g.
Code: [Select]
<a href="">Exclude this text also</a>
Thanks
« Last Edit: February 23, 2010, 11:56:24 AM by Andy WFUK »

Offline Andy WFUKTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Re: Do not match if link tags?
« Reply #4 on: February 25, 2010, 09:43:18 AM »
Any idea Sader or anyone else? Bit stuck on this!