Error when using str_replace() convert from ' to " - php

<?php
$str='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search='"';
$replace=''';
$string= str_replace($search,$replace,$str);
echo $string;
?>
When I echo $string is result no convert from " to ' , how to fix it

In your original code, you were passing an undefined variable ($str) to the str_replace function
I believe your intention was to pass the $tr variable to the str_replace function.
Also, in your $replace variable, I've changed it to having double quotes (") between the character.
<?php
$tr='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search= '"';
$replace= "'";
$string = str_replace($search,$replace,$tr);
echo $string;
?>
Change the $str to $tr as $str is not defined. It will also help if you
Change your $replace to have double quotes
$replace= "'";

You have the text stored in $tr not $str.
<?php
$tr='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search='"';
$replace="'";
$string= str_replace($search,$replace,$tr); // Changed to $tr
echo $string;
?>
And make sure to escape the ' in $replace='''; (or use "")

$replace='''; is incorrect. You need to escape the second '.
Use either
$replace = "'" so that the single quote does not interfere, or
$replace = '\'' which escapes the middle single quote.

Escape the quote.
$replace='\'';
// or
$replace="'";

Related

Echo a string literal from PHP that includes both quotes and apostrophes

I have a little bit more complicated row than I can handle.
I am echoing and I got lost in the " ', could someone help me out and tell me how to have this line correctly?
echo '<td style="text-align: center"><a onclick=" window.open('/edit.php?id=' . $row['id'] . ','_self')"><img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/></a></td>';
Escape single quotation?
echo '...<a onclick=" window.open(\'/edit.php?id=...';
Edit
To show single quotation in string wrapped with single quotation, you need to escape it, like this
echo 'Hello \' world';
So your code should be
echo '<td style="text-align: center"><a onclick=" window.open(\'/edit.php?id=' . $row['id'] . '\',\'_self\')"><img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/></a></td>';
Use the heredoc syntax to create the string and your " and ' can be used freely inside the string.
See manual about heredoc http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Example of heredoc:
https://3v4l.org/SJTBO
I don't know about a third quotation mark in code.
But, using "heredoc" syntax is really usefull when you want/need to keep both the single and double quotes unescaped.
Using it, your code should look like this:
echo <<<EOD
<td style="text-align: center">
<a onclick="window.open('/edit.php?id='{$row['id']}', '_self')">
<img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/>
</a>
</td>
EOD;
Think about using { } around your variables to make them more visible.
See documentation about it here: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

PHP preg_replace: how to replace spaces inside html tag

I need to replace spaces inside a tag to other symbol. For example:
<p class="hero big" style="color: inherit; border: none">Super big hero <br />example Yeah</p>
to
<p~class="hero~big"~style="color:~inherit;~border:~none">Super big hero <br~/>example Yeah</p>
I'm new to regexp and don't know to start with. I'm able to replace spaces everywhere, but not inside a tag.
Ho to do it? Could you please provide working php code?
You can use (*SKIP)(*F) to skip outside tags like this:
$str = preg_replace('~>[^<]*(*SKIP)(*F)|\s+~','~', $str);
See demo at eval.in
Try this
$text = htmlspecialchars('<p class="hero big" style="color: inherit; border: none">Text <br />Text</p>', ENT_QUOTES);
$text = preg_replace('/\s+/', '~', $text);
echo $text;
Note:- Treat html tags and main text separately
try this, It will replace spaces inside tag with ~
<?php
$data = '<p class="hero big" style="color: inherit; border: none">Super big hero <br />example Yeah</p>';
$replace = preg_replace('/(<[^>]*)\s+([^<]*>)/', '$1~$2', $data);
echo $replace;
?>
Thanks to PHP preg_replace: how to replace text between tags?, here's a solution:
<?php
$s = '<p class="hero big" style="color: inherit; border: none">Super big hero <br />example Yeah</p>';
$text = preg_replace_callback('#(<)([^>]+)(>)#', function($matches){
return $matches[1].str_replace(" ", "~", $matches[2]).$matches[3];
}, $s);
?>

Php str_replace with &quote not working

my code is:
$retval = preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);
$imgreal = str_replace(""", "\"", $image['src']);
if ($retval=="0") $imgthumb = ""; else $imgthumb = "<img align='left' src='".$imgreal."' width=100 />";
I need to extract an img from a string, but this gave me all time:
"http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214
How can I change those &quotes to normal chars "?
I tried htmlspecialchars_decode but that not worked to.
edit: the string coming from az sql table.
original string from mysql:
<p><img style="float: left; margin: 3px;" src="http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214" /></p>
<p>todik púőúőóüsztotodik púőúőóüsztotodik
string created by TinyMCE
coding in the table: latin2_hungarian_ci
Why does everybody always want to do these kind of things with a regex?
Simply:
$data='<p><img style="float: left; margin: 3px;" src="http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214" /></p>
<p>todik púőúőóüsztotodik púőúőóüsztotodik';
$a=explode('src="',$data);
if(count($a)<2)
{
echo 'no image';
die;
}
$p=strpos($a[1],'"',0);
if($p===false){
echo 'no quote found';
die;
}
$url=substr($a[1],0,$p);
echo $url;
Output:
http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg

Find all <img> tags and change style attribute

i am trying to find a way to get the tag from a string and remove the style attribute .
After that i want to add my own style and keep the following text..
For example, i have:
<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p>
end the endresult should be:
<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 100%;" /></p><p> </p><p>
I have unsuccesfully tried regex but it seems like i am too dumb to understand its functionality...
Every help would be appreciated!
Greetings from Greece
Jim
You can do it by regex like that:
$str = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p>';
$newStr = preg_replace('#<img (.+) style="(.+)" />#isU', '<img $1 style="width: 100%" />', $str);
To remove height or some other property:
$string = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p' ;
$pattern = "/height:\s*\d*\s*(px|%);*/" ;
$new = preg_replace($pattern,"", $string) ;
echo htmlentities($new) ;
Or remove all style things and replace with own ones:
$string = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p' ;
$pattern = "/style=[\'\"][^\"|^\']*[\'\"]/" ;
$own_style = "style='width: 50%'" ;
$new = preg_replace($pattern, $own_style, $string) ;
echo htmlentities($new) ;
Generally using RegExp on HTML tags is kinda bad thing and should be avoided, but in some situations may be applicable.

Print string with a php variable in it

For you guys, I imagine this will be easy.
<div class="vote_pct" style="width: $widthpx;">
I want the variable to be $width and for it to have px on the end.
If i put a space, it doesnt work.
If i put them together, it treats it as one big variable name.
Could I ask someone the correct snytax to achieve this?
Thanks
$bla = '<div class="vote_pct" style="width: '.$width.'px;">';
or
$bla = "<div class=\"vote_pct\" style=\"width: ${width}px;\">";
If you mix PHP and HTML you can do:
//PHP in HTML
<div class="vote_pct" style="width: <?php echo $width; ?>px;">
HTML in PHP
print '<div class="vote_pct" style="width: ' . $width . 'px;">';
echo '<div class="vote_pct" style="width: '.$width.'px;">';
or
$width = 5;
echo "<div class=\"vote_pct\" style=\"width: {$width}px;\">";
<?php echo sprintf('<div class="%s" style="width: %spx">','vote_pct',$width);?>

Categories