Jump to content

str_replace with array


acccc

Recommended Posts

Hello guys this is my first post. :)

 

Just a quick problem I've encountered, I'm trying to str_replace an array and string together and wondering how it can be done?

 

$text = "this is @value1 and this is @value2";

$array1 = array ( 0 => 'value1', 1 => 'value2'); // just an example

$at_replace = str_replace (" . @ " $array1, " . # " $array1, $text);

 

So im basically trying to replace all instances in $text where each word in $array1 starting with '@' will be replaced with a '#'.

 

Also will this be better done with a loop?

 

Any help or direction is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/231605-str_replace-with-array/
Share on other sites

you could use the implode function to turn it into string then str_replace then explode into array again.

 

e.g

 

<?php

$array = //array
implode(" " . $array);
str_replace($replace, $array);
explode(" " . $array);

?>

 

it should work, just wrote it of the top of my head.

 

@matthew, it doesnt seem to do the trick.

 

As an example of what I want to do, your name 'matthew' would be in the array $array1

 

You post a comment and you write "@john, how are you?"

 

Then the str_replace would replace where '@' and 'john' is (if john is in the array $array1) with a hyperlink to johns profile, similar to what you get in twitter hash tags, youtube comments or facebook.

 

I hope this makes sense.

Do only want to replace the '@' at the beginning of the words? So

This is @test@

would become

This is #test@

or do you want to replace all the '@' symbols in a string, so the test string becomes

This is #test#

 

If it's the first variant, you can do something like:

<?php
function at_to_hash($str) {
   return (ltrim(str_replace(' @',' #',' ' . $str)));
}
//
//tests
//
echo at_to_hash('this is @test') . "<br>\n";  // result: this is #test
echo at_to_hash('@this@') . "<br>\n"; // result: #this@
echo at_to_hash('@this is @a test') . "<br>\n"; // result: #this is #a test
?>

 

Ken

Do only want to replace the '@' at the beginning of the words? So

This is @test@

would become

This is #test@

or do you want to replace all the '@' symbols in a string, so the test string becomes

This is #test#

 

If it's the first variant, you can do something like:

<?php
function at_to_hash($str) {
   return (ltrim(str_replace(' @',' #',' ' . $str)));
}
//
//tests
//
echo at_to_hash('this is @test') . "<br>\n";  // result: this is #test
echo at_to_hash('@this@') . "<br>\n"; // result: #this@
echo at_to_hash('@this is @a test') . "<br>\n"; // result: #this is #a test
?>

 

Ken

 

 

Thanks for the speed reply Ken, it worked well and replaces all the @ into #

 

I wanted it to replace the '@'s with '<a href='example.php?user=' so as to make the area where @ is present a link to the user profile.

 

I was wondering now, how to add ''>@username</a>' at the end of the word so as to complete the hyperlink. (Think of what you see in youtube's new commenting system, or facebook's '@' function. This is what im trying to achieve. Ive been trying different things but it doesn't seem to work (the easiest way looked to be to concatenate the str_replace e.g.

 

 str_replace ("@" . $array1, "<a href='example.php?user=" . $array1 . "'>@username</a>", $text) 

 

but I dont think this works for arrays and im not 100% sure on how to concatenate correctly.

Archived

This topic is now archived and is closed to further replies.

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