preg_replace URLs but not images - php

$title = $_POST['title'];
$post = stripslashes($_POST['TextArea']);
$link = preg_replace('"(http://www\S+)"','$1', $post);
echo $link;
After submit my form the above script replace all links inside textarea
and the result for images is to be broken.
Is there a way to replace links but not images?
While url works perfect the result for images in browser is
<img src="http://.../myimage.jpg" height="150" width="150">
Thank you

preg_replace('"(?<!src=[\"\'])(http://www\S+)"','$1', $text)
This will only convert http://www links that are not preceded by src=" or src='.

preg_replace('/(?<!src=[\"\'])(http(s)?:\/\/(www\.)?[\/a-zA-Z0-9%\?\.\-]*)(?=$|<|\s)/','$1', $text);
This is the correct way because previous solution does not finish url when necessary

Related

Remove specific image from a string

I want to remove specific image from string.
I need to remove Image with specific width and height.
I have tried this, but this will remove first image.
$description = preg_replace('/<img.*?>/', '123', $description, 1);
I want to remove any/all image(s) with specific width and height.
E.g. Remove this image <img width="1" height="1" ..../>
I suggest that you move away from using regex expressions to parse (or manipulate) HTML, because it's not a good idea, and here's a great SO answer on why.
For example, by using Peter's approach (preg_match_all('~<img src="(.+?)" width="(.+?)">~is', $content, $return);), you are assuming that all your images start with <img, are followed by the src, and then contain the width=, all typed exactly like that and with those exact whitespace separations, and those particular quotes. That means that you will not capture any of these perfectly valid HTML images that you want to remove:
<img src='asd' width="123">
<img src="asd" width="123">
<img src="asd" class='abc' width="123">
<img src="asd" width = "123">
While it's of course perfectly possible to catch all these cases, do you really want to go through all that effort? Why reinvent the wheel when you can just parse the HTML with already-existing tools. Take a look at this other question.
Made a little example for you
<?php
$string = 'something something <img src="test.jpg" width="10" height="10" /> and something .. and <img src="test.jpg" width="10" height="10" /> and more and more and more';
preg_match_all('~<img(.+?)width="10"(.+?)height="10"(.+?)/>~is', $string, $return);
foreach ($return[0] as $image) {
$string = str_replace($image, '', $string);
}
echo $string;
I got the solution:
$description = preg_replace('!<img.*?width="1".*?/>!i', '', $description);

Find certain pattern of string and replace content , in scraped content from a site

I have some scraped content like
<figure><a href="/fzesny/sample.html">
<img alt="playerpr120x76" src="/assets/small.jpg"><span class="number">1</span>
<figcaption>Some Name</figcaption></a>
I need to change the href value to "#" and append scraped contents URL in the image src.
<a href="">
and image tag must have the server URL appended
<img src="www.example.com/assets/small.jpg"
For image i used
$find = "/assets";
$replace = 'http://www.example.com/assets';
echo str_replace($find,$replace,$full_content,$i)
How can i remove the href link ?
You can remove the content of your href tag with preg_replace :
$str= 'Hey !';
$str = preg_replace("/href=\".*\"/", "href=\"\"", $str);
echo $str;
Output : Hey !

Replace img src using str_replace doesnt work

Hi I'm trying to modify the src of a image that I uploaded by ckfinder and I want a full path I see other answers here but I dont know why my code doesn't work ,look at it please
$html = '<p> <img alt="" src="/deconsultas/javascript/ckfinder/files/images/xxx.jpg"/></p>';
$dato = str_replace("src=&quot/deconsultas/javascript","a",$html);
echo $dato;
Where is the error please ?
Escape quote with slash
use
\"
instead of
&quot
I think you need this.
$html = '<p> <img alt="" src="/deconsultas/javascript/ckfinder/files/images/xxx.jpg"/></p>';
$dato = preg_replace("/\/deconsultas\/javascript/","a",$html);
echo $dato;
just change str_replace("src=&quot/deconsultas/javascript","a",$html) to str_replace("src=\"/deconsultas/javascript","a",$html)

find image with specific src using preg_replace

I have some text with images within it. I want to replace specific images within the text with something else.
i.e. the text contains an a youtube img url that I want to replace with the actual video link.
<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">
and replace it with the youtube Iframe code:
<iframe title="'.$id.'" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/'.$id.'" frameborder="0"></iframe>
my function looks like this:
function replacelink($link) {
$find= ("/<img src=[^>]+\>/i");
$replace = youtube("\\2");
return preg_replace($find,$replace);
}
What do I need to change in the regex to do the above?
Your regex is looking for <img src=, but there is a class attribute between img and src. Using $find= '/<img.*src=[^>]+>/i'; corrects the problem; however, this illustrates why you shouldn’t use regex to parse HTML.
You wrote:
I have some text with images within it.
If the text you’re referring to is actually HTML, then there are better alternatives to using regex for this.
Update
I believe this is what you’re looking for.
<?php
function replacelink($text) {
$replace = '<iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" <iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/$2" frameborder="0"></iframe>';
$find = '/(<img.*?alt="([\da-z]+)".*?>)/i';
return preg_replace($find, $replace, $text);
}
$imagestr = '<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">';
echo replacelink($imagestr);
?>
There’s no need for a separate youtube() function.
If you want to replace more than one image, use preg_replace_all() instead of preg_replace().
The following regex would get all the images with a specific url. I not sure if this is what you wanted.
<img [^>]*?src="url"[^>]*?>
Previous anwser would fail if there were more than one image.

Simple question about htmlentities and a small editing on my code for better <img src> results

I have a foreach loop (see below) that encodes the values to htmlentities() and then it is send through the form as a url.
for example www.domain.com/file.php?title=XXX&pics=XXX&content=XXX
This is my code:
foreach($results as $image){
$encodedpics = '<img src="'.$image.'"><br>';
echo htmlentities($encodedpics, ENT_QUOTES);
Then in the submitted form, I decode using html_entity_decode() and appeared inside the textarea. The problem is that the url images appear like these and can not be displayed later.
<img src=\"http://www.mypic.jpg\">
<img alt=\"\" src=\"http://www.mypic.png\">
<img border=\"0\" src=\"http://mypic.jpg\">
My question is how can I have only a <img src="www.mypic.jpg"> without \ or borders or alt?
Thank you all.
run htmlentities() on $image, not the entire string.
foreach($results as $image){
$encodedpics = '<img src="'.htmlentities($image, ENT_QUOTES, 'UTF-8).'"><br>';
echo $encodedpics;

Categories