1

I have an associative array of links like this

$my_links = [
       'city 1' => 'http://link1',
       'city 2' => 'http://link2',
       'Head Office' => 'http://link3'
    ];

and some html like this. The html is dynamically generated by a script ( wordpress blog content).

<p>
   You can visit our stores at City 1 
   and City 2, 
   or visit our Head office.
</p>

Required Output: Make clickable links using indices of above array

<p>
   You can visit our stores at <a href="http://link1">City 1</a>
   and <a href="http://link2">City 2</a>, 
   or visit our <a href="http://link3">Head office</a>.
</p>

How to achieve this using PHP and/or JQuery?

2
  • 1
    do like this <p> You can visit our stores at <a href="<?php echo $my_links['city 1'];?>">City 1</a> and <a href="<?php echo $my_links['city 2'];?>">City 2</a>, or visit our <a href="<?php echo $my_links['Head Office'];?>">Head office</a>. </p> Commented Mar 29, 2019 at 12:41
  • Thanks for quick reply and Edit. The html is dynamically generated by a script ( from a wordpress blog content). Updated my question. Commented Mar 29, 2019 at 12:53

4 Answers 4

3
<?php 

$my_links = [
       'city 1' => 'http://link1',
       'city 2' => 'http://link2',
       'Head Office' => 'http://link3'
    ];


$str = "<p>
   You can visit our stores at City 1 
   and City 2, 
   or visit our Head office.
</p>";


foreach ($my_links as $link_title => $link) {
    $str = str_ireplace($link_title,"<a href='$link'>".ucwords($link_title)."</a>",$str);
}

echo $str;

Loop over your $my_links. Find the link title present in the string and use str_ireplace() to replace the link title with the anchor tags.

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

1 Comment

Thanks a lot for this!
3

Just use the array references:

<p>
   You can visit our stores at <a href="<?php echo $my_links['city 1']; ?>">City 1</a>
   and <a href="<?php echo $my_links['city 2']; ?>">City 2</a>, 
   or visit our <a href="<?php echo $my_links['Head Office']; ?>">Head office</a>.
</p>

1 Comment

Thanks for quick reply. The html is dynamically generated by a script ( from a wordpress blog content). Updated my question.
2

If your content is coming from PHP, use PHP's str_replace() function.

This function's three parameters search the purpose:

  1. search: string or array

  2. replace: string or array

  3. actual string: string

If search and replace are arrays, their elements count should be same.

Now, find the string segments like City 1, City 2 and Head Office and replace them by adding <a href="... to them.

Code:

<?php
$my_links = [
       'city 1' => 'http://link1',
       'city 2' => 'http://link2',
       'Head Office' => 'http://link3'
    ];
$content = '<p>
   You can visit our stores at City 1 
   and City 2, 
   or visit our Head office.
</p>';

$find = ['City 1', 'City 2', 'Head office'];
$replace = [
'<a href="'.$my_links['city 1'] . '">City 1</a>',
'<a href="'.$my_links['city 2'] . '">City 2</a>',
'<a href="'.$my_links['Head Office'] . '">Head Office</a>',
 ];
echo  str_replace($find, $replace, $content);
?>

A Quick Update:

Please use str_ireplace() instead of str_replace() discussed above as we have case insensitive comparison e.g. city 1 vs City 1. This function works the same way as str_replace(), only it is case insensitive.

Comments

1

You can try this

<p>
   You can visit our stores at <a href="<?=$my_links['city 1']?>">City 1</a>
   and <a href="<?=$my_links['city 2']?>">City 2</a>, 
   or visit our <a href="<?=$my_links['Head Office']?>">Head office</a>.
</p>

1 Comment

Thanks for quick reply. The html is dynamically generated by a script ( from a wordpress blog content). Updated my question.

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.