Jump to content

Do not match if link tags?


Andy WFUK

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

I noticed a problem with the above solution. If the string has a link already in it, or it has

<br />

or

<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.

<a href="exclude_this_path" title="and exclude this"

and b) when the text is within link tags e.g.

<a href="">Exclude this text also</a>

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.