Andy WFUK Posted February 22, 2010 Share Posted February 22, 2010 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 Quote Link to comment Share on other sites More sharing options...
sader Posted February 22, 2010 Share Posted February 22, 2010 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); Quote Link to comment Share on other sites More sharing options...
Andy WFUK Posted February 22, 2010 Author Share Posted February 22, 2010 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 Thanks again - I must really lean more REGEX! Quote Link to comment Share on other sites More sharing options...
Andy WFUK Posted February 23, 2010 Author Share Posted February 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
Andy WFUK Posted February 25, 2010 Author Share Posted February 25, 2010 Any idea Sader or anyone else? Bit stuck on this! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.