I have the following problem and I am not really good in regular expressions, so please, could somebody help me out?
<div class="bulletPoints">
<div>
<img src="../images/test_f01.jpg" alt="test_f01"/>1
<img src="../images/test_f02.jpg" alt="test_f02"/>2
<img src="../images/test_f03.jpg" alt="test_f03"/>3
<img src="../images/test_f04.jpg" alt="test_f04"/>4
<img src="../images/test_f05.jpg" alt="test_f05"/>5
<img src="../images/test_f06.jpg" alt="test_f06"/>6
<img src="../images/test_f07.jpg" alt="test_f07"/>7
<img src="../images/test_f08.jpg" alt="test_f08"/>8
<img src="../images/test_f09.jpg" alt="test_f09"/>9
<img src="../images/test_f09.jpg" alt="test_f09"/>10
<img src="../images/test_f09.jpg" alt="test_f09"/>11
<!-- and so on -->
</div>
</div>
I need to replace the plain text string after the image tag inside of each anchor tag. The string is always a number from 0 - 99. The IDs of the anchor tags are auto generated and the title attribute, too. I don't know how to reach only the number on all tags. I need to replace it with an empty string ''. Could somebody help me and explain how I can do that??
Thank you very very much!!
Try
preg_replace('/(<img[^>]+>)(\d+)/', "\\1", $text);
This will replace the image tag + text after the image tag with only the image tag.
you can replace />[0-9]{,2}< with /><
test with sed:
echo 'yourtext'|sed -r 's#/>[0-9]{,2}<#/><#g'
output:
<div class="bulletPoints">
<div>
<img src="../images/test_f01.jpg" alt="test_f01"/>
<img src="../images/test_f02.jpg" alt="test_f02"/>
<img src="../images/test_f03.jpg" alt="test_f03"/>
<img src="../images/test_f04.jpg" alt="test_f04"/>
<img src="../images/test_f05.jpg" alt="test_f05"/>
<img src="../images/test_f06.jpg" alt="test_f06"/>
<img src="../images/test_f07.jpg" alt="test_f07"/>
<img src="../images/test_f08.jpg" alt="test_f08"/>
<img src="../images/test_f09.jpg" alt="test_f09"/>
<img src="../images/test_f09.jpg" alt="test_f09"/>
<img src="../images/test_f09.jpg" alt="test_f09"/>
<!-- and so on -->
</div>
</div>
preg_replace('|>[0-9]{1,2}<|','><',$html);
Related
My end goal is to create a gallery of Images that when clicked link to an external website. This needs to be done through Advanced Custom Fields, so I made a repeater that has the image and link in the same row:
link1 | cover_image1
link2 | cover_image2
Right now I'm inserting this code into a text editor inside my webpage. I also imported some short-code from here, that allows me to use %ROW% as an iterator.
"attachments" is the parent repeater and "link" and "cover_image" are the children.
[acf_repeater field="attachments"]
external url = [acf field ='attachments_%ROW%_link']
image url = [acf field ='attachments_%ROW%_cover_image']
<a href =[acf field ='attachments_%ROW%_link'] >
<img src = [acf field ='attachments_%ROW%_cover_image'] width="300" height="214" />
</a>
[/acf_repeater]
The webpage renders this:
Where the broken image contains this code:
<img src="[acf" field="attachments_0_cover_image" ]="" width="300" height="214">
I think [acf field ='attachments_%ROW%_cover_image'] in <img> isn't resolving all the way to the url, as external url = and image url = both render the correct urls.
Wordpress also converts my code to this after saving, so its probably a syntax error?
[acf_repeater field="attachments"]
external url = [acf field = attachments_%ROW%_link]
image url = [acf field = attachments_%ROW%_cover_image]
<a href="[acf">
<img src="[acf" width="300" height="214" />
</a>
[/acf_repeater]
I'm not sure how to properly convert [acf field ='attachments_%ROW%_cover_image'] to a url in the <img> and I could use some help on the proper syntax. Thank you so much for you help!
html for the attribute per Arian:
<div class="fl-module fl-module-rich-text fl-node-5d4926759d7aa"
data-node="5d4926759d7aa">
<div class="fl-module-content fl-node-content">
<div class="fl-rich-text">
<p>Agenda: https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/cute-cat-photos-1593441022.jpg?crop=0.669xw:1.00xh;0.166xw,0&resize=640:*</p>
<p>Video Links: </p>
<p>Thumbnails: </p>
<p></p>
<p>external url = https://www.youtube.com/watch?v=uHKfrz65KSU<br>
image url = http://wordpress.local/wp-content/uploads/Thumbnail_1.png</p>
<p><a href="[acf" field="attachments_0_link" ]=""><br>
<img src="[acf" field="attachments_0_cover_image" ]="" width="300" height="214"><br>
</a></p>
<p><br></p>
<p>external url = https://www.youtube.com/watch?v=X2lIovmNsUY<br>
image url = http://wordpress.local/wp-content/uploads/Thumbnail_2-1.png</p>
<p><a href="[acf" field="attachments_1_link" ]=""><br>
<img src="[acf" field="attachments_1_cover_image" ]="" width="300" height="214"><br>
</a></p>
<p><br></p>
<p>external url = https://www.youtube.com/watch?v=hDJkFLnmFHU<br>
image url = http://wordpress.local/wp-content/uploads/Thumbnail_3-1.png</p>
<p><a href="[acf" field="attachments_2_link" ]=""><br>
<img src="[acf" field="attachments_2_cover_image" ]="" width="300" height="214"><br>
</a></p>
<p><br></p>
</div>
</div>
</div>
Not a wordpress guy, but seems like wordpress prevents expanding/executing shortcodes with parameters within html attributes, maybe this could work as workaround if you can put php code there:
<?php
$link = do_shortcode("[acf field ='attachments_%ROW%_link']");
$cover_img = do_shortcode("[acf field ='attachments_%ROW%_cover_image']");
?>
<a href="<?= $link ?>">
<img src="<?= $cover_img ?>" width="300" height="214" />
</a>
Actually i found comment on this:
It appears it's using the same quotes for the HTML attribute and the shortcode's attributes that create problems.
at here
so this may work too:
<a href="[acf field ='attachments_%ROW%_link']">
<img src="[acf field ='attachments_%ROW%_cover_image']" width="300" height="214" />
</a>
or without shortcode quotes:
<a href="[acf field=attachments_%ROW%_link]">
<img src="[acf field=attachments_%ROW%_cover_image]" width="300" height="214" />
</a>
last option i can think of is creating parameter-less shortcodes like this:
function acflink_shortcode() {
return do_shortcode("[acf field ='attachments_%ROW%_link']");
}
add_shortcode('acflink', 'acflink_shortcode');
function acfimage_shortcode() {
return do_shortcode("[acf field ='attachments_%ROW%_cover_image']");
}
add_shortcode('acfimage', 'acfimage_shortcode');
then using in editor like:
<a href="[acflink]">
<img src="[acfimage]" width="300" height="214" />
</a>
Not all src attribute in html, Exactly the div ID begins with "post_message_"
HTML CODE
<div id="another_div">
<img src="http://domain.com/folder/somfilename.jpg" border="0" alt="">
</div>
<div id="post_message_412">
<img src="http://domain.com/folder/filename1.jpg" border="0" alt="">
</div>
<div id="post_message_413">
<img src="http://domain.com/folder/filename2.jpg" border="0" alt="">
</div>
<div id="post_message_414">
<img src="http://domain.com/folder/filename3.jpg" border="0" alt="">
</div>
<div id="post_message_415">
<img src="http://domain.com/folder/filename4.jpg" border="0" alt="">
</div>
Get image tag's url with in the div ID begins with "post_message_" (Not like 'another_div' id name) using php preg_match() or preg_match_all() or preg_split() or any other possible solutions.
after that i want to get urls from an array
Assuming the HTML is the same structure as in your example, you can use: <div\s*id="post_message_[^"]+">\s*<img src="([^"]+)
It's only long because of the literals.
\s* matches 0+ whitespace characters
[^"]+ matches characters that aren't "
How can i change this example when i just got the <image> tag and i want to add link tags around them and the rel lightbox attribute as well?
i cant figure it out. I'm just not good with regular expressions at all.
the example
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>';
So in my case i have <img src="...." class="...." alt=".....">
and i need <img src="...." class="...." alt=".....">
How would i change this?
Thanks for help.
It's unclear what you exactly want to do here, but I would utilize DOM for this task instead.
$doc = DOMDocument::loadHTML('
<img src="www.foo.com/1.gif" class="foo" alt="...">
<img src="www.bar.com/1.jpg" class="bar" alt="...">
<img src="example.com/2.jpg" class="example" alt="...">
');
foreach ($doc->getElementsByTagName('img') as $node) {
$link = $node->ownerDocument->createElement('a');
$a = $node->parentNode->insertBefore($link, $node);
$a->setAttribute('href', $node->getAttribute('src'));
$a->setAttribute('rel', 'lightbox');
$a->setAttribute('title', 'some title');
$a->appendChild($node);
}
echo $doc->saveHTML();
Output:
<img src="www.foo.com/1.gif" class="foo" alt="...">
<img src="www.bar.com/1.jpg" class="bar" alt="...">
<img src="example.com/2.jpg" class="example" alt="...">
I'm looking for a way to modify a HTML Document based on a number of occurring tags. For example
<img src="" alt="" />1></a>
<img src="" alt="" />1></a>
<img src="" alt="" />1></a>
<img src="" alt="" />1></a>
Must become:
<div>
<img src="" alt="" />1></a>
<img src="" alt="" />1></a>
<img src="" alt="" />1></a>
<img src="" alt="" />1></a>
</div>"
...
That is, for every 4 occurencies of <img> there must be a div tag added to the page. For those of you wondering why I can't simply write this - those tags are dinamically generated via other scripts.
Furthermore, only 1 img might be present, or 1231. However, the PHP script must add <div> tag at the begginig of every first <img>and </div> on every 4th and then another <div>. If there are only 3,2 or 1 instances, then </div> must come after the last one.
Any ideas ?
Please excuse the numerous edits, I was having problem with Firefox not showing the editor.
var tags = $('tag');
var group = new Array();
var i, g = 0;
$.each(tags, function() {
if(i == 4) {
g++;
group[g] = $('<div></div>').appendTo($('body'));
}
$(this).appendTo(group[g]);
i++;
});
Or something similar to that
I have a url let's say : https://stackoverflow.com/example1/
and images tags
<img f="/example2"></img>
<img f="example3"></img>
<img f="http://example4.com"></img>
<img f="https://example5.com"></img>
and here is my regex code for preg_replace:
$regex = "-(src\s*=\s*['\"])(((?!'|\"|http://|https://).)*)(['\"])-i";
so if I use this code in the tags above the output will be :
<img f="https://stackoverflow.com/example1//example2></img>
<img f="https://stackoverflow.com/example1/example3></img>
<img f="http://example4.com/"></img>
<img f="https://example5.com/"></img>
but what I want is /example2 and example3 get replaced too but for /example2 it gets only https://stackoverflow.com/ and for example3 it gets https://stackoverflow.com/example1
ps : I know how to get domain using parse_url. and f= is src ^^