I am trying to replace smileycodes by a img tag.
I want to replace this:
:blush:
by:
<img src='images/blush.png' />
"blush" is a variabele, so it can be any smiley.
I have to replace everything between the colons. I am not familiar with regex.
Can you help me?
For multiple replacements you could use str_replace with arrays instead of strings to search for, having an array for smiley-codes to be replaced by values of the second array. But you need to configure all values in arrays what is kind of static.
Another solution was to loop over possible smiley-codes and do a str_replace for each of it:
$possibleCodes = array('blush', 'smiley2', 'smiley3');
foreach($possibleCodes as $code) {
str_replace(':'.$code.':', "<img src='images/".$code.".png'", $yourString);
}
This requires to have the image name same as the smiley-code.
Why use regex if you aren't familiar with it? It is very simple and easy to do without regex.
str_replace(":blush:", "<img src='images/blush.png' />", $myString);
You can use preg_replace like as
preg_replace('~(:blush:)~','<img src="images/blush.png" />',$your_string);
Edited
If you had an array of string to replace then you can simply use str_replace like as
$find_array = array('blush', 'smiley2', 'smiley3');
$replace_array = ['<img src="images/blush.png" />','<img src="images/smiley2.png" />','<img src="images/smiley3.png" />']
str_replace($find_array,$replace_array,$your_string);
Related
Let's say I have a the following string:
{{Hi}}, This {{is}} {{Debby}}.
I want to replace the text inside {{ANYTHING}} with variables passed to a function. For example, if the function is change_values('Hola', 'was', 'Antonio').
The result would be:
Hola, This was Antonio.
First word or character surrounded by {{}} was replaced by the first parameter of change_values(). Similarly, the second word or character was replaced by the second parameter and so on.
To be clear, the values Hi, is and Debby can change. The passed parameters can also change. The only thing that is consistent is that First {{}} would be replaced by first parameter and so on.
I was planning on using str_replace() originally but the text keepschanging each time. I also thought about using regex but cannot figure out how to do the replacements sequentially.
Any help would be appreciated.
A few more examples,
{{Fiona}} is a lucky {{girl}}.
will become
Mike is a lucky man.
I am using {{}} as identifiers in the original string to make it easy to figure out what needs to be replaced. If this can create an issue, I am open to other (better) solutions.
If you're using PHP5.6 or later, this function will do what you want. It uses ... to pack all the replacements into an array, and then preg_replace to replace all the strings surrounded by {{ and }} with the replacements. By using the limit parameter to preg_replace, we prevent the pattern replacing all the {{}} strings with the first value in the replacements array.
function change_values($string, ...$replacements) {
return preg_replace(array_fill(0, count($replacements), '/{{[^}]+}}/'), $replacements, $string, 1);
}
echo change_values('{{Hi}}, This {{is}} {{Debby}}.', 'Hola', 'was', 'Antonio');
echo change_values('{{Fiona}} is a lucky {{girl}}.', 'Mike', 'man');
Output:
Hola, This was Antonio.
Mike is a lucky man.
$input = '{{Fiona}} is a lucky {{girl}}.';
$replaceArray = ['Mike', 'man'];
$expectedOut = 'Mike is a lucky man.';
preg_match_all('/({{\w+}})/', $input,$matches);
$out = str_replace($matches[0], $replaceArray, $input);
if($out === $expectedOut){
print_r($out);
}
I have a string
$k="My name is Alice[1]";
What i want is to remove "[1]" from my string Using minimum steps,So that sentence will look like
$k="My name is Alice";
So antything that come inside [] should be removed.
Use this:
$text = preg_replace('/\[[0-9]+\]/','',$text);
You can use preg_replace for this.
echo preg_replace('/\[[0-9]+\]/','',$str);
Do this:
$k="My name is Alice[1]";
$k = preg_replace('/\[[0-9]+\]/','',$k);
I have the following code:
this code finds all html tags in a string and replaces them with [[0]], [[1]] ,[[2]] and so on.(at least that is intented but not workinng);
$str = "some text <a href='/review/'>review</a> here <a class='abc' href='/about/'>link2</a> hahaha";
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",$str, $out, PREG_OFFSET_CAPTURE);
$count = 0;
foreach($out[0] as $result) {
$temp=preg_quote($result[0],'/');
$temp ="/".$temp."/";
preg_replace($temp, "[[".$count."]]", $str,1);
$count++;
}
var_dump($str);
This code finds all the tags in a string and replaces them with [[0]], [[1]] and [[2]] and so on. I have used preg_match_all with PREG_OFFSET_CAPTURE.
The output of preg_match_all is as expected. However, preg_replace, substr_replace, and str_replace do not work when substituting the tags with [[$count]].
I have tried all three string replacement methods and none of them work. Please point me in the right direction.
Can something in php.ini cause this?
Thanks in advance.
preg_replace does not substitute $str. Assign it to the string again:
$str = preg_replace($temp, "[[".$count."]]", $str);
Also, I'm not sure what you want exactly, but this I changed some things in the code, which seems to be what you were tying to do. I changed the regex a bit, especially the (.*?) part to ([^<>]+).
the problem may be in this line
foreach($out[0] as $result) {
change it to this
foreach($out as $result) {
because i think you are accessing an index that doesn't exists
Need to remove everything between .jpg and > on all instances like these below:
.jpg|500|756|20121231-just-some-image-3.jpg)%>
.jpg|500|729|)%>
.jpg|500|700|)%>
.jpg|500|756|test-43243.jpg)%>
So everything becomes .jpg>
Any suggestions using preg_replace?
preg_replace('/\.jpg[^>]+>/', '.jpg>', $your_string);
$str = '.jpg|500|756|20121231-just-some-image-3.jpg)%>';
preg_replace('/[^\.jpg][^>]+/', '', $str);
I'm extracting twitter user's profile image through JSON. For this my code is:
$x->profile_image_url
that returns the url of the profile image. The format of the url may be "..xyz_normal.jpg" or "..xyz_normal.png" or "..xyz_normal.jpeg" or "..xyz_normal.gif" etc.
Now I want to delete the "_normal" part from every url that I receive. How can I achieve this in php? I'm tired of trying it. Please help.
Php str_replace.
str_replace('_normal', '', $var)
What this does is to replace '_normal' with '' (nothing) in the variable $var.
Or take a look at preg_replace if you need the power of regular expressions.
The str_ireplace() function does the same job but ignoring the case
like the following
<?php
echo str_ireplace("World","Peter","Hello world!");
?>
output : Hello Peter!
for more example you can see
The str_replace() function replaces some characters with some other characters in a string.
try something like this:
$x->str_replace("_normal","",$x)
$s = 'Posted On jan 3rd By Some Dude';
echo strstr($s, 'By', true);
This is to remove particular string from a string.
The result will be like this
'Posted On jan 3rd'
Multi replace
$a = array('one','two','three');
$var = "one_1 two_2 three_3";
str_replace($a, '',$var);
string erase(subscript, count)
{
string place="New York";
place erase(0,2)
}