Jump to content

Complicated String Replace


mctoys

Recommended Posts

Hi Guys

 

I'm sure this is really simple for you experts but it's proving to be my nemesis! I have an array of words: eg.

 

$words = array('Apples','Bananas','Cherries');

 

and would like to search a string of text for these words and create link tags around them based on a simple template, <a href="#">$word_found</a>

 

$text = "I love eating apples and bananas but hate cherries!";

 

So i would like to return a string like this:

 

$text = "I love eating <a href=#>apples</a> and <a href="#">bananas</a> but hate <a href="#">cherries!</a>";

 

Now, I need it to do a case insensitive search, but just wrap tags around the words it finds, leaving the case as it was found in the string. I want to avoid using a function where I have to list all the replacements like this:

 

function replace_text($text){

    $replace = array('apples' => '<a href="#">apples</a>','bananas' => '<a href="#">bananas</a>');

    $text = str_ireplace(array_keys($replace), $replace, $text);

    return $text;

}

 

because it seems very messy and long winded. Also it's not case insensitive.

 

It seems like there must be a way of simply searching for the words in my array and wrapping tags around the found words.

 

Please can anyone help!?

 

Many Thanks

Dan

Link to comment
Share on other sites

Hmm Ok - so no immediate replies - maybe I'm asking the wrong questions!

 

So i've been playing around with the script and would be happy to use the str_replace method if I can dynamically populate the array which handles the keywords and tags. Here is what I have:

 

<?php

 

$words = array('Ben','Tim','John');

$text = 'Me and my friends John Tim and Ben went to the park.';

$replace = array();

 

foreach($words as $word) {

array_push($replace[$word], "<a href=\"#\">".$word."</a>");

}

 

    $newtext = str_replace(array_keys($replace), $replace, $text);

 

?>

 

The problem is, my array_push doesn't work, it outputs this:

 

Array

(

    [ben] =>

    [Tim] =>

    [John] =>

)

 

Me and my friends  and  went to the park.

 

Any ideas?

Link to comment
Share on other sites

You can use preg_replace to do this. The following can be modified to do what you are asking -

 

<?php
$str = "this is my test string and string is very long with so many words";
$search = "my|string|with|words";
$str = preg_replace("/($search)/is","<a href='/index.php?word=$1'>$1</a>",$str);
echo $str;
?>

 

$search in the above can be produced by imploding your $words array using the '|' character.

Link to comment
Share on other sites

You can use preg_replace to do this. The following can be modified to do what you are asking -

 

<?php
$str = "this is my test string and string is very long with so many words";
$search = "my|string|with|words";
$str = preg_replace("/($search)/is","<a href='/index.php?word=$1'>$1</a>",$str);
echo $str;
?>

 

$search in the above can be produced by imploding your $words array using the '|' character.

 

Oh wow - that's a thousand times easier than what I was trying to do!!

Thanks so much!!

 

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.