Laravel string replace item - php

How to i replace '[' charachter to span class = "someclass" .I wrote this code but it gives me only text
$replaced = str_replace(
array('[', ']'),
array("<span class"."='innovation_color innovation_color-services".">","</"."span".">"),
$string
);

you are missing some quotes :
$replaced = str_replace(
['[', ']'],
["<span class"."='innovation_color innovation_color-services'".">","</"."span".">",
$string
);

Related

str_replace() doesn't work using "(" and "[" PHP

This is my simple code function in php
function replaceCharact($input,$action){
$output_1 = str_replace('(', "%11%", $input);
$output_2 = str_replace(')', '%12%', $output_1);
$output_3 = str_replace('[', '%13%', $output_2);
$output_4 = str_replace(']', '%14%', $output_3);
$output_5 = str_replace('"', '%15%', $output_4);
$output_6 = str_replace('/', '%16%', $output_5);
$output_7 = str_replace('"\"', '%17%', $output_6);
$output_8 = str_replace('!', '%18%', $output_7);
$output_9 = str_replace('<', '%19%', $output_8);
$output_10 = str_replace('>', '%20%', $output_9);
return $output_10;
}
Only the "!"($output_8) change to %19%. The others output display nothing. Can you help me with this?
To simplify mass replacements using an array, try this...
$replacement = array(
'(' => "%11%",
')' => '%12%',
'[' => '%13%',
']' => '%14%'
// etc etc
);
$string = str_replace( array_keys( $replacement ), $replacement, $string );
https://3v4l.org/kmXZp

Replace a single quote ( ' ) with another quote (’) str_replace

I am trying to replace a single quote (') with another quote (’), but I can't seem to get anything to work.
Also, how can I make this work on multiple strings ($text, $title, $notice)?
input: don't
output: don’t
I am trying this:
$text = str_replace(array("'",'"'), array('’'), $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $_POST['text']));
$text = str_replace("'" ,"’",$text);
$text = str_replace("'" ,"’",$text);
$text = str_replace(array("'"), "’", $text);
$text = str_replace(array("\'", "'", """), "’", htmlspecialchars($text));
$text = str_replace(array('\'', '"'), '’', $text);
$text = str_replace(chr(39), chr(146), $text);
$text = str_replace("'", "&ampquot;", $text);
None of this works.
When you use an array as replacements, give as many replacements as you give needles. Else use a simple string.
<?php
$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), '’', $text);
echo $text;
$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), array('’', '`'), $text);
echo $text;
?>
To perform that task on multiple variables you could do
<?php
$text = 'Peter\'s cat\'s name is "Tom".';
$title = 'Peter\'s cat\'s name is "Tom".';
$notice = 'Peter\'s cat\'s name is "Tom".';
foreach([&$text, &$title, &$notice] as &$item)
$item = str_replace(array("'",'"'), '’', $item);
echo "text: $text<br>title: $title<br>notice: $notice";
?>
I tried using preg_replace() and it worked perfectly first time:
$text = "Someone told me about a 'bacon and cheese sandwich'";
$text = preg_replace("#\'#", '’', $text);
echo $text;
Output
Someone told me about a ’bacon and cheese sandwich’
Give that a go.

Regular expression for finding multiple patterns from a given string

I am using regular expression for getting multiple patterns from a given string.
Here, I will explain you clearly.
$string = "about us";
$newtag = preg_replace("/ /", "_", $string);
print_r($newtag);
The above is my code.
Here, i am finding the space in a word and replacing the space with the special character what ever i need, right??
Now, I need a regular expression that gives me patterns like
about_us, about-us, aboutus as output if i give about us as input.
Is this possible to do.
Please help me in that.
Thanks in advance!
And finally, my answer is
$string = "contact_us";
$a = array('-','_',' ');
foreach($a as $b){
if(strpos($string,$b)){
$separators = array('-','_','',' ');
$outputs = array();
foreach ($separators as $sep) {
$outputs[] = preg_replace("/".$b."/", $sep, $string);
}
print_r($outputs);
}
}
exit;
You need to do a loop to handle multiple possible outputs :
$separators = array('-','_','');
$string = "about us";
$outputs = array();
foreach ($separators as $sep) {
$outputs[] = preg_replace("/ /", $sep, $string);
}
print_r($outputs);
You can try without regex:
$string = 'about us';
$specialChar = '-'; // or any other
$newtag = implode($specialChar, explode(' ', $string));
If you put special characters into an array:
$specialChars = array('_', '-', '');
$newtags = array();
foreach ($specialChars as $specialChar) {
$newtags[] = implode($specialChar, explode(' ', $string));
}
Also you can use just str_replace()
foreach ($specialChars as $specialChar) {
$newtags[] = str_replace(' ', $specialChar, $string);
}
Not knowing exactly what you want to do I expect that you might want to replace any occurrence of a non-word (1 or more times) with a single dash.
e.g.
preg_replace('/\W+/', '-', $string);
If you just want to replace the space, use \s
<?php
$string = "about us";
$replacewith = "_";
$newtag = preg_replace("/\s/", $replacewith, $string);
print_r($newtag);
?>
I am not sure that regexes are the good tool for that. However you can simply define this kind of function:
function rep($str) {
return array( strtr($str, ' ', '_'),
strtr($str, ' ', '-'),
str_replace(' ', '', $str) );
}
$result = rep('about us');
print_r($result);
Matches any character that is not a word character
$string = "about us";
$newtag = preg_replace("/(\W)/g", "_", $string);
print_r($newtag);
in case its just that... you would get problems if it's a longer string :)

php regex bbcode returns blank

I have this bbcode tag "remover" which should remove bbcode tags from my test text.
All i get is nothing. Just blank page where should be the text replaced with html tags.
Whats wrong with it. And maybe anyone have some better script to share.
$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);
$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "$1", $str );
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );
return $str;
}
The following is your code, with some code in front of it (to make sure any errors are shown) and some code at the back (that actually calls your function).
If this doesn't work for you, your problem is not here, unless you don't have a working PCRE.
error_reporting(-1); ini_set('display_errors', 'On');
$str = 'This [b]is just[/b] a [i]test[/i] text!';
function forum_text($str)
{
$str = htmlspecialchars($str);
$str = preg_replace( "#\[url\](?:http:\/\/)?(.+?)\[/url\]#is", "$1", $str );
$str = preg_replace( "#\[img\](?:http:\/\/)?(.+?)\[/img\]#is", "<img src=\"http://$1\" />", $str );
$str = preg_replace( "#\[b\](.+?)\[/b\]#is", "<strong>$1</strong>", $str );
$str = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>$1</i>", $str );
$str = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>$1</u>", $str );
return $str;
}
echo forum_text($str);

String Replace In PHP

i have a long string like
$str = "this is [my] test [string] and [string] is very long [with] so many pwords]"
i know str_replace function, but when i replace
$str = str_replace( "[", "<a href=\"/story.php?urdu=",$str;
$str = str_replace( "]", "\"></a>",$str;
i got this result
but i want this result for each word in []
test
You should use preg_replace() for this one.
Code
<?php
function replace_matches($matches) {
$text = htmlspecialchars($matches[1]);
$url = urlencode($matches[1]);
return "{$text}";
}
$str = "this is [my] test [string] and [<script>alert(1)</script>] is very long [with] so many pwords]";
$str = preg_replace_callback("%\[(.+?)\]%", "replace_matches", $str);
echo $str;
?>
Working Example
$str = preg_replace_callback(
'/\[([^\]]*)\]/',
function($matches) {
return sprintf('%s',
urlencode($matches[1]),
htmlspecialchars($matches[1]));
},
$str);

Categories