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

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.