From a variable containing tags layout as <img>
I want to replace all the image tags and content by the same tag concatenated with <br />
for example:
<img src="mon image" style="width:80px; float: right;">
replaced by
<img src="mon image" style="width:80px; float: right;"> <br />
I succed to replace the img tag with a character but not like I want
Try with this regular expression:
$string = '<img src="mon image" style="width:80px; float: right;">';
$pattern = "/(\<img\b[^>]*>)/";
$replacement = '${1}<br/>';
echo preg_replace($pattern, $replacement, $string);
Also #LawrenceCherone has right your IMG should be like this:
<img src="mon image" style="width:80px; float: right;"/>
But the regular expression also works with that tag declaration.
Related
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);
?>
I have a grid of images that is generated on a page with PHP. This is the HTML for each image:
Echo "<img src= '$link[Image_Link] . /133x100' title='$row[Item_Name]' alt='$just_name' onClick='addValue(". $credit_value .")' border=0 style='position: relative; top: 0; left: 0;'/>"
This is with the ID:
Echo "<id='img1' img src= '$link[Image_Link] . /133x100' title='$row[Item_Name]' alt='$just_name' onClick='addValue(". $credit_value .")' border=0 style='position: relative; top: 0; left: 0;'/>"
The src, title, alt, etc attributes are being correctly set.
If I try to add any kind of id, for example: <id="img" src=.... it seems to break the HTML code and the image will no longer appear.
Why is this happening? I need the images to have an id so I can use them with jquery functions.
This code is wrong:
<id='img1' img src=....
It should look like this:
<img id="img1" src= .../>
You can try like this
<div id="myId">
<img src="http://placehold.it/400x300" id="myImgId">
</div>
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 "es 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
i want to replace img tags in a string. Replacing an img tag and add for example an a[href] tag works fine with a dom parser but this won't help me as i have to alter the tags in an existing string of html. I know that altering html with regex isn't a good idea but i can't figure out how to change img tags as whole in a string of html. Beside that i need to wrap the tag in an a[href] where the [href] is provided via a property of the img tag.
<img class="myclasses" width="100" src="mysource" data-image-src="another_src">
After the "transformation" any img tag found should look like this:
<img [...] src="http://myexample.me[mysource]">
I got it to work if one image is in the string but failed to handle two images.
I hope someone could help me out :)
You can probably achieve what you need using preg_replace_callback and then looping through the image's attributes in the callback function.
So for example, given this test string:
$content= <<<HTML
<img alt="image-alt-2" src="image-path" style="width: 20px; height: 15px; border: 1px solid red;" title="image-title" data-image-src="another_src" />
<p>Some other tags. These shouldn\'t be changed<br />Etc.</p>
<img alt="image-alt-2" src="image-path-2" style="width: 35px; height: 30px;" title="another-image-title" data-image-src="somewhere_else" />
HTML;
We can then match out the images and call our replacement function:
$content= preg_replace_callback('/<img ((?:[-a-z]+="[^"]*"\s*)+)\/>/i', 'replaceImage', $content);
For my example I'm just removing the data-image-src attribute and using it to create the link, everything else is left as it was:
function replaceImage($matches) {
// matches[0] will contain all the image attributes, need to split
// those out so we can loop through them
$submatches= array();
$donelink= false;
$count= preg_match_all('/\s*([-a-z]+)="([^"]*)"/i', $matches[1], $submatches, PREG_SET_ORDER);
$result= '<img ';
for($ndx=0;$ndx<sizeof($submatches);$ndx++) {
if ($submatches[$ndx][1]=='data-image-src') {
// Found the link attribute, prepend the link to the result
$result= "<a href=\"{$submatches[$ndx][2]}\">$result";
$donelink= true; // We've added a link, remember to add the closing </a>
}
// You can handle anything else you want to change on an attribute-by-attribute basis here
else {
// Something else, just pass it through
$result.= $submatches[$ndx][0];
}
}
return "$result/>".($donelink?'</a>':'');
}
Running that on the sample content gives:
<img alt="image-alt-2" src="image-path" style="width: 20px; height: 15px; border: 1px solid red;" title="image-title"/>
<p>Some other tags. These shouldn\'t be changed<br />Etc.</p>
<img alt="image-alt-2" src="image-path-2" style="width: 35px; height: 30px;" title="another-image-title"/>
Hope that helps!
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.