I have a page of images wrapped with links. Essentially I want to remove the links surrounding images, but keep the image tags in tact.
eg. I have:
<img src="image1.jpg" alt="image 1">
and I want:
<img src="image1.jpg" alt="image 1">
I tried this code I found in my research, but it leaves a stray </a> tag.
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" ></a>}'),
array('<img','" />'),
$content
);
I have no idea when it comes to regular expressions so can someone please fix my code? :-)
You can use <a.*?(<img.*?>)<\/a> to match and replace with $1
See DEMO
$content = preg_replace('/<a.*?(<img.*?>)<\/a>/', '$1', $content);
by your provided regex it seems you are using wordpress and want to remove hyperlinks from content.
if you are using wordpress then you can also use this hook to remove hyper links on images from content.
add_filter( 'the_content', 'attachment_image_link_remove_filter' );
function attachment_image_link_remove_filter( $content ) {
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" /></a>}'),
array('<img','" />'),
$content
);
return $content;
}
here is the another function that will also work.
function attachment_image_link_remove_filter($content)
{
$content =
preg_replace(array('{<a[^>]*><img}', '{/></a>}'), array('<img', '/>'), $content);
return $content;
}
add_filter('the_content', 'attachment_image_link_remove_filter');
OR you can use also Demo
$string = '<img src="image1.jpg" alt="image 1">';
$result = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $string);
echo $result; // this will output "<img src="image1.jpg" alt="image 1">"
I think that
$content = preg_replace('/<a\s+href=[^>]+>(<img[^>]+>)<\/a>/', '$1', $content);
is a better solution.
Ex : https://regex101.com/r/3ozruM/1
Because #karthik manchala's solution https://regex101.com/r/ETkE58/1 doesn't work in such a case.
Related
I'm using the following code to remove the sizes added by Wordpress to medias' filenames.
function replace_content($content) {
$content = preg_replace('/-([^-]*(\d+)x(\d+)\. ((?:png|jpeg|jpg|gif|bmp)))"/', '.${4}"', $content);
return $content;
}
add_filter('the_content','replace_content');
How to change the regex to apply it only to the href attribute value?
Folowing regex with preg_replace() function
$replaced_content = preg_replace( '#<img[^>]*?src[\s]?=[\s]?[\'"]?([^\'">]*?(https|http|\/\/)[^\'">]*?(png|jpeg|jpg|gif|bmp))[^\'" >]*?)[\'" ][^>]*?>#',
'<img src="$1">', $content );
cleans this awful img tag
<img ttl='Ren src = https://cdn.wpbeginner.com/wp-content/uploads/2015/01/rename-on-save.png' alt="Rena width=520" height="344" wp-image-25391">
to this clean and nice code
<img src="https://cdn.wpbeginner.com/wp-content/uploads/2015/01/rename-on-save.png">
So, I am using this to replace BBCode to HTML:
$text = htmlspecialchars($text);
$advanced_bbcode = array(
'#\[quote](\r\n)?(.+?)\[/quote]#si',
'#\[url](.+)\[/url]#Usi');
$advanced_html = array(
'<blockquote class="quote">$2</blockquote>',
'<a rel="nofollow" target="_blank" href="$1">$1</a>');
$text = preg_replace($advanced_bbcode, $advanced_html,$text);
echo nl2br($text);
public static function nl2br($var)
{
return str_replace(array('\\r\\n','\r\\n','r\\n','\r\n', '\n', '\r'), '<br />', nl2br($var));
}
This works fine if I only have 1 quote, but If I use multiple quotes like: [quote][quote][quote]first[/quote]second[/quote]end[/quote]
I expect to get:
<blockquote class="quote"><blockquote class="quote"><blockquote class="quote">first</blockquote>second</blockquote>end</blockquote>
But because it takes the first [/qoute] it will turn into:
<blockquote class="quote">[quote][quote]first</blockquote>second[/quote]end[/quote]
I've looked it up but I cant find anything that is working for me. I am new to this kind of stuff.
Thanks.
Make replace until there is BBCode in the string
do {
$text = preg_replace($advanced_bbcode, $advanced_html,$text,-1,$c);
} while($c);
demo
With security and also a deprecated function; what would be the easiest and most secure way to call a function in a find and replace?
There are four find and replace modules which can be inserted within content [album][/album], [img][/img], [youtube][/youtube], or [vimeo][/vimeo].
Using the function I put together so far Images, YouTube and Vimeo were a no brainer. The Album no so much. I would like to call a function based on parameters that are passed.
I tried altering this function into a preg_replace_callback and that just mocks up everything. Is there any alternatives?
function FormatModules($text) {
$find = array(
'~\[album\](.+?)\[/album\]~s',
'~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
'~\[youtube\](.+?)\[/youtube\]~s',
'~\[vimeo\](.+?)\[/vimeo\]~s'
);
$replace = array(
'GenerateAlbum($1)', // call a PHP function
'<img src="$4" width="$1" height="$2" alt="$3" />',
'<iframe src="http://www.youtube.com/embed/$1"></iframe>',
'<iframe src="https://player.vimeo.com/video/$1"></iframe>'
);
return preg_replace($find, $replace, $text);
}
If you wanted to call a function on more than one replacement or you wish to set up your script for future modifications so that functions can be called on the replacement parameter, you might entertain preg_replace_callback_array().
Otherwise, I'd say make a preg_replace_callback() involving the first elements of $find and $replace then run a call of preg_replace() on the remaining elements.
Code: (Demo)
function GenerateAlbum($match) {
return "<div class=\"album\>Do whatever: " . strtoupper($match[1]) . "</div>";
}
function FormatModules($text) {
$text = preg_replace_callback('~\[album\](.+?)\[/album\]~s', "GenerateAlbum", $text);
$find = array(
'~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
'~\[youtube\](.+?)\[/youtube\]~s',
'~\[vimeo\](.+?)\[/vimeo\]~s'
);
$replace = array(
'<img src="$4" width="$1" height="$2" alt="$3" />',
'<iframe src="http://www.youtube.com/embed/$1"></iframe>',
'<iframe src="https://player.vimeo.com/video/$1"></iframe>'
);
return preg_replace($find, $replace, $text);
}
echo FormatModules("[vimeo]test1[/vimeo]\n\n[album]test2[/album]\n\njust text\n\n[img width=50 height=100 alt='sumpin good']http://www.example.com/image.gif[/img]\n\n[youtube]test3[/youtube]");
Output:
<iframe src="https://player.vimeo.com/video/test1"></iframe>
<div class="album\>Do whatever: TEST2</div>
just text
<img src="http://www.example.com/image.gif" width="50" height="100" alt="'sumpin good'" />
<iframe src="http://www.youtube.com/embed/test3"></iframe>
What I want
If the URL in the string contains a .jpg at the end of the URL (not the string) then it should make an image from it with preg_replace else make a normal link.
so for example:
If I have http://www.example.com/images/photo.jpg then it should replace with:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
The problem:
The URL is replaced with a link in any way and my regex isn't working :( .
What I have tried:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "$1", $content));
Try this
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '$1', $content);
}
return $content;
}
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
You don't need lookaround. Just go with
$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);
I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.
This worked for me.
$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg
In the figure above, router R1 has two point-to-point . ';
$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);
echo $parse_img;
Suyash
I have a code that wraps every image on posts with an external div (for share buttons when hovering on images).
The thing is, when I want to write the_permalink(); in the function.php, it's wrapped inside a ' or a " in the a href tags.
That causes share links look like this:
https://plus.google.com/share?url=%3C?php%20the_permalink();%20?%3E
This is the code in function.php:
function breezer_addDivToImage( $content ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$the_url = the_permalink();
$replacement = '<div class="imgWrap">
$1
<div class="imgDescription">
<div class="theShareLinks">
<img src="http://localhost/mySite/wp-content/uploads/2014/08/dfc2.png" />
</div>
</div>
</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
add_filter( 'the_content', 'breezer_addDivToImage' );
These are the lines from the code above^ that are wrapped in the " " :
Changing:
to
should work.