0

I have a string of words in an array, and I am using preg_replace to make each word into a link. Currently my code works, and each word is transformed into a link.

Here is my code:

$keywords = "shoes,hats,blue curtains,red curtains,tables,kitchen tables";

$template = '<a href="http://domain.com/%1$s">%1$s</a>';

$newkeys = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-z]+)\b/is",    sprintf($template, "\\1"), $keywords);

Now, the only problem is that when I want 2 or 3 words to be a single link. For example, I have a keyword "blue curtains". The script would create a link for the word "blue" and "curtains" separately. I have the keywords separated by commas, and I would like the preg_replace to only replace the text between the commas.

I've tried playing around with the pattern, but I just can't figure out what the pattern would be.

Just to clarify, currently the output looks as follows:

  <a href="http://domain.com/shoes">shoes</a>,<a     href="http://domain.com/hats">hats</a>,<a href="http://domain.com/blue">blue</a>     <a href="http://domain.com/curtains">curtains</a>,<a     href="http://domain.com/red">red</a> <a href="http://domain.com/curtains">curtains</a>,<a href="http://domain.com/tables">tables</a>,<a href="http://domain.com/kitchen">kitchen</a> <a    href="http://domain.com/tables">tables</a>

While I want to achieve the following output:

<a href="http://domain.com/shoes">shoes</a>,<a      href="http://domain.com/hats">hats</a>,<a href="http://domain.com/blue      curtains">blue curtains</a>,<a href="http://domain.com/red curtains">red    curtains</a>,<a href="http://domain.com/tables">tables</a>,<a      href="http://domain.com/kitchen tables">kitchen tables</a>
2
  • 1
    put your expected output also in your code pls Commented May 21, 2015 at 18:38
  • Hi, thank you for quick reply. I just edited my question added the current and expected output Commented May 21, 2015 at 18:49

1 Answer 1

0

A little bit change in preg_replace code and your job will done :-

$keywords = "shoes,hats,blue curtains,red curtains,tables,kitchen tables";

$template = '<a href="http://domain.com/%1$s">%1$s</a>';

$newkeys = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-z ' ']+)\b/is",    sprintf($template, "\\1"), $keywords);

OR

$newkeys = preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-z' ']+)\b/is",    sprintf($template, "\\1"), $keywords);

echo $newkeys;

Output:- http://prntscr.com/77tkyb

Note:- I just added an white-space in your preg_replace. And you can easily get where it is. I hope i am clear.

Matching white-space along with words is missing there in preg_replace and i added that only.

Sign up to request clarification or add additional context in comments.

1 Comment

So mark it as your answer, so that other's can get advantage from this post. thanks:)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.