str_replace - doesn't work when using "< >" - php

I've got a problem with str_replace.
I have scraped a page with curl and now I want to replace an href id like this:
(all variables are present)
$search_result = str_replace("<href=\"?run=".$id."\">", "<href=\"?run=1111\">", $search_result);
The problem is that when I use the "<" and ">" characters in the str_replace it will not work.
Can anyone tell me why?
I also tried this (which does work like expected):
$test = "< something >";
$test = str_replace("<", "(", $test);
echo $test;

i'm not sure to have understand your question...
$test = "< something >";
$test = str_replace("<", "(", $test);
$test = str_replace(">", ")", $test);
echo $test;
this give me this output
( something )
so it works!
and doing a little mod to your code works fine on me, just try this:
$link = "hello world";
$search_result = str_replace('<a href="?run=1">', "<a href=\"?run=1111\">", $link);
echo $search_result;

Use this regular expression to replace the multiple occurance , as < and . are escape characters we have to escape them as shown below
value.replace(/>/g, '(');

Related

How to replace 'n\' from a string (not \n) in Php

I have a string which contains n\ by mistake (as imported from csv). So,I just want to replace n\ with \n.
Possible conditions : n\,\n\n, n\\n,n\n\
$string = "hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n";
Try this. It will remove all the possible matches as per your question:-
$str = "main_string_goes_here";
$replace = "n\,\n\n,n\n\,\nn\,n\\n";
$arr = explode(",",$replace);
foreach($arr as $value)
{
str_replace($value,"\n",$str);
}
Happy Coding :-)
Use this:
str_replace("n\","\n",$string);
Here we search for the string, find the value "n\" and then replace the value with "\n". Update this example with your conditions.
Try like this
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$string = str_replace('n\\n\\','&new*',$string);
$string = str_replace('\\n\\n','&old*',$string);
$string = str_replace('n\\','\\n',$string);
$string = str_replace('&new*','\\n\\n',$string);
echo $string = str_replace('&old*','\\n\\n',$string);
Live demo : https://eval.in/904353
As other says to replace "n\" to "\n" will not work. You need to escape \ backslash also
Use this:
<?php
echo $string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);
echo $text = str_replace('n', '', $new_str);
?>
You can try this.
$string = 'hello\n how n\n\ are you?\n\nis everything\nn\ok buddy, n\ where have you been. \n';
echo str_replace('n\\', '\n',$string );

php echo string html with php-variables, can't break the php string

I'm trying to echo a phpstring-message. This php string consists of html and php variables and comes form a database and i can't change that data.
$name = 'John';
$str = '<b>Hi {$name},</b><br/>How are you?';
echo $str;
So i'm trying to replace the php string, but it doesn't work. This is my code:
$str = str_replace('{', '\' . ', $str);
$str = str_replace('}', ' . \' ', $str);
I get: <b>Hi' . $name. ',</b><br/>How are you?
How do i get the string like this?
<b>Hi John,</b><br/>How are you?
Thank you in advance
Just do it like this, you won't be able to replace it with a concatenation:
echo str_replace('{$name}', $name, $str);
EDIT:
If you don't know the name of the variable just use this:
echo preg_replace('/\{(.*?)\}/', $name, $str);
it's already implemented in PHP, you can directly write the variable in double quote like this:
echo "<b>Hi $name,</b><br/>How are you?";
or for some more complex variables:
echo "<b>Hi {$user->name},</b><br/>How are you?";

How can i insert text into html tag in php string?

i have been dealing with this for awhile and i could not figure out how to do the following in PHP:
$string = "this is test <pre>somrthing in <p>pharagraph</p></pre> dont know tell me <pre>value33 kooo ok</pre> this is php regress <pre>teeth value</pre> ok how";
function get_innercode($string) {
preg_match_all("#<pre>(.*?)</pre>#", $string, $foo);
echo implode("\n", $foo[1]);
}
$insert1=get_innercode($string);
$insert2=" 2 place me in the pre tag"; // you can ignore this
$string="this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me <pre>WHERE $insert2 SHOULD BE</pre> ok how ";
how can i do this?
Please note i cannot do
$string="some text here <pre> WHERE $insert1 SHOULD BE</pre> some more text here <pre>WHERE $insert2 SHOULD BE</pre> ok how";
because i am getting $insert1 and $insert2 from $string to modify. i need to place it back in there where they come from.
thank you
It's pretty easy to do:
$insert1 =" 1 place me in the pre tag";
$insert2 =" 2 place me in the pre tag";
$string = "some text here <pre>{$insert1}</pre> some more text here <pre>{$insert2}</pre> and may be some more text or ";
You can echo a variable from inside a string when it's wrapped around double quotes. This does not work with single quotes.
Edit: This might be what you are looking for then:
$string = "This my <code>awesome</code> code";
$string = preg_replace('/<code>(.*?)<\/code>/', '<pre>$1</pre>', $string);
In php you can concatenate strings with a "." before and after, example:
$var1 = "hello";
$var2 = "What's up";
$concat = "Hey, ".$var1." ,".$var2."<br>";
echo $concat;
Similar methodology here. This allows for larger areas to be filled
$insert[0] = "Some Text";
$insert[1] = "Some More Text";
function Parser($content){
$i = 0;
while (preg_match_all('`\<pre\>(.*?)\</pre\>`', $content, $matches)) {
foreach ($matches[0] as $key => $match) {
//$innertext = $matches[1][$key]; //replace with your preferred filler
$innertext = $insert[$i]; //such as this
$replacement = '<pre>' . trim($innertext) . '</pre>';
$content = str_replace($match, $replacement, $content);
$i++;
}
}
}
Parse your line(s) of content like so:
Parser("this is test <pre> WHERE $insert1 SHOULD BE</pre> dont know tell me <pre>WHERE $insert2 SHOULD BE</pre> ok how");
This is untested so may have a couple of bugs in it.
Try this,
I am adding div tag before each pre content. You can also modify content as needed by using $matches[1]
$string_new = preg_replace_callback(
'#<pre>(.*?)</pre>#',
function ($matches) {
return "<div>".$matches[0]."</div>";
},
$string
);
echo $string_new;

Replace every space in the beginning of the string

Using php preg_replace.
Tried:
$test = " 123";
$test = preg_replace("/^\s/","?",$test);
echo '|' . $test;
Outputs:
|? 123
What I need:
|???????123
Also tried another variants, but they all replace only FIRST space or ALL-IN-ONE...
Spaces inside of the string or at the end of the string - should NOT be touched.
You can probably do this much easier without regular expressions, utilizing strspn:
$whitespaceCount = strspn($test, " \t\r\n");
$test = str_repeat("?", $whitespaceCount).substr($test, $whitespaceCount);
<?php
$test = " 12 3 s";
$test = preg_replace_callback("/^([\s]*)([^\s]*)/","mycalback",$test);
echo '|' . $test;
function mycalback($matches){
return str_replace (" ", "?", $matches[1]).$matches[2];
}
?>
output:
|??????12 3 s
why you just dont try this:
echo '|' . preg_replace('/\s/','?',' 123');
Try this : remove ^ from pattern which checks for the beginning of the string, So what happens is it only replaces the space at the beginning (just one space)
$test = " 123";
$test = preg_replace("/\s/","?",$test);
echo '|' . $test;
$test = " 123";
$test = preg_replace("/^[ ]|[ ]/","?",$test);
echo '|' . $test;

Looking for some REGEX help in PHP

I have a string:
[COLOR=gray]A bunch of text.[/COLOR]
And I would like to write a preg_replace that removes everything between "[COLOR=gray]" and "[/COLOR]" -- if it's possible to remove those tags as well, that's great, otherwise I can do a simple replace afterward.
$str = 'dfgdfg[COLOR=gray]A bunch of text.[/COLOR]dfgdfgdfgfg';
$str1 = preg_replace('/\[COLOR=gray\].*\[\/COLOR\]/',"",$str);
echo $str1;
OR
if COLOR is not always gray
$str = 'dfgdfg[COLOR=gray]A bunch of text.[/COLOR]dfgdfgdfgfg';
$str1 = preg_replace('/\[COLOR=\w+\].*\[\/COLOR\]/',"",$str);
echo $str1;

Categories