How to replace data from dynamic <a href="...." /> hyperlinks in php? - php

I got blocks of div hyperlinks as string in $code. This is example of blocks found in $code:
<div id="" class="thumbo">
</div>
i want to replace everything before 4th slash with ./doit.php.id=
For example i want change this :
<div id="" class="thumbo">
</div>
to
<div id="" class="thumbo">
</div>
The problem is that data before last 4th slash is dynamic and wonder how i can replace them all with ./doit.php?id=?

Related

Setting the target of an onclick event to a specific location

I split my body content in two parts. The left one has a map and buttons. When I click on the button, I get the result from Arad.php in another window.
How can I set the target to the second half (split right) of my body?
<body>
<div class="split left">
<div class="centered">
<h2>Button on Image</h2>
<p>Add a button to an image:</p>
<div class="container">
<img src="Harta_Romaniei.jpg" alt="Harta_Romaniei" style="width:100%">
<button class="btnarad"; onclick= "window.location.href='Arad.php'"; Target="split right">Arad</button>
<button class="btntimisoara">Timisoara</button>
</div>
</div>
</div>
<div class="split right">
<div class="centered">
<h2>Information</h2>
// I want the information here!
</div>
</div>
</body>
It doesn't have much to do with PHP, you should really do it with HTML + JavaScript.
The window.href.location will always redirect you to another route, and will never do what you want.
You'll need something like this: Simple Load an HTML page from javascript based on window width

php search and replace <h2> to <h1> in my view source [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
I have the following html
<!-- START: .paragraph-content -->
<div class="paragraph-content">
<div class="container"><div class="row"><div class="col-sm-10">
<!-- START: .paragraph-columns -->
<div class="paragraph-columns">
<div class="field-wysiwyg">
<div data-quickedit-field-id="paragraph/167/field_mt_body/en/default" class="field field--name-field-mt-body field--type-text-long field--label-hidden field__items">
<div class="field__item">
<h2> </h2>
<h2> </h2>
<h2>INNOVATION.</h2>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
</div>
</div>
<!-- END: .paragraph-columns -->
</div></div></div>
</div>
<!-- END: .paragraph-content -->
I want to capture where the html begins with <div class="paragraph-content">
in that block, I want to change the <h2> to <h1>
so the end result will look like this:
<!-- START: .paragraph-content -->
<div class="paragraph-content">
<div class="container"><div class="row"><div class="col-sm-10">
<!-- START: .paragraph-columns -->
<div class="paragraph-columns">
<div class="field-wysiwyg">
<div data-quickedit-field-id="paragraph/167/field_mt_body/en/default" class="field field--name-field-mt-body field--type-text-long field--label-hidden field__items">
<div class="field__item">
<h2> </h2>
<h2> </h2>
<h1>INNOVATION.</h1>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
</div>
</div>
<!-- END: .paragraph-columns -->
</div></div></div>
</div>
<!-- END: .paragraph-content -->
I have tried it with this regex pattern but nothing works:
'/(?:<h2((?!\s").*?)?>)(.*?)(?:<\/h2>)/si'
If you have the HTML page as a string variable, accomplished by:
$fileStr = file_get_contents('HTML_FILE.htm');
You can then find the start of the section you are after by using the text "<!-- START: .paragraph-content -->" and the end of the section of the string by using the text "<!-- END: .paragraph-content -->".
Having the start and end of the string, we can extract the portion of the $fileStr in which we want to run our regular expression against.
The regular expression required to find the string you want to change is:
<h2>.{2,}<\/h2>
The issue you have to to extract and replace the <h2> and </h2> with <h1> and </h1> whilst retaining everything in between these.
Doing that isn't going to be a simple neat solution. I would do a loop which would look for <h2>, then find if there is any alphanumerics between that and the closing </h2>, then extract the contents between the two if there is, replacing the tags appropriately.
Whilst not providing you with code to cut and paste, I hope I've given you something to ponder.
Regex works as a finite state machine, it has no way to parse recursive things, like XML tags that might contain other XML tags.
Basically, you cant match exactly the closing tag that matches the opening tag, because that requires recursion, which is not possible in finite state machines (there is Python module regex that has recursion and some other implementations, but this is not true regex).
For your problem exaclty you need a whole top-down recursive parser or some tool that works with XML/HTML specifically.
Just replacing the h2 tags with h1 in the whole regex'ed string is as simple as <(/?)h2> -> <$1h1> though.

Preg matching random text from html source code using PHP

I have website and it's source html code looks something like below.
<li class="item" xx-href-xx="http://xx.xx/s/randomtext/randomtext?NOTradnomtext" yy-href-gg="http://xx.xx/X/RANDOMTEXTWHATIWANT/STILLRADNOMTEXTWHATIWANT?NOTradnomtext" data="212123134" data-title="TITLE">
<a class="front" href="#" xx-href="http://xx.xx/s/randomtext/randomtext?NOTradnomtext">
<img src="http://photo.jpg" alt="">
<div class="cock">
<div class="action"></div>
</div>
</a>
<div class="label">
<div>
<h3 class="title">Example</h3>
<p>2013-10-25 : 03:35</p>
</div>
</div>
</li>
... And so on same kind of classes (only titles and texts changing) ...
How to preg_match yy-href-gg="http://xx.xx/X/TEXTWHATIWANT/TEXTWHATIWANT?NOTradnomtext from all of those records and include also title for result. So result should look in this case something like that
Example
TEXTWHATIWANT/TEXTWHATIWANT
Example2
TEXTWHATIWANT/TEXTWHATIWANT
and so on.
Sorry if my post is little bit unclear, I should to go sleep..

Replacing content between two div tags - PHP

I'm trying to replace content between two div tags using str_replace but I'm unsure how.
The content will be
<div class="profile-details">
<div class="username">Paradigm</div>
<div class="dob">01/01/2015</div>
</div>
What I want to do is replace the content between the <div class="profile-details">content </div> tags. The content is variable depending on the user profile.
Assuming this is a HTML string.
This would be my approach
echo preg_replace('/<div class="username">.+?</div>/im', '<div class="username">Special Username<\/div>', $string) ;

jquery find http elements in plain text and convert it to a link

I don't know if this is possible but I have a piece of plain text which always includes:
http://www.royalmail/portal/etc/etc/track.php?trackNumber=123345323
When dragged to my page from the database this obviously does not render as a link. Is there any way to select this piece of text (ie via the http://) and at the most basic wrap it with anchor tags - or more complexly - retrieve the address, wrap the address with anchor tags, and change the original http:// text to something more understandable such as 'Track Parcel'
It should be noted that there could be numerous tracking references looped out at once.
My containing HTML is as follows
<div class="message_holder">
<div class="mess_head" style="width:110px;">
<p><strong>Sender: </strong><? echo $row11['sender'];?></p>
</div>
<div class="mess_head" style="width:450px;">
<p><strong>Subject: </strong><span class="red"><? echo $row11['subject'];?></span></p>
</div>
<div class="mess_head" style="width:150px;">
<p><strong>Date Sent: </strong><? echo date('d/m/Y, H.ia', $row11['date_sent']);?></p>
</div>
<div class="mess_head" style="width:200px;">
<p><strong>Message ID: </strong><? echo $row11['message_id'];?></p>
</div>
<div class="mess_body">
<p><? echo html_entity_decode($row11['message']);?></p>
</div>
</div>
The plain text links will all show up in the 'mess_body' class. I have tried using html_entity_decode() but this did not do the trick. If there is an easy way to do this via PHP rather than JQuery it could be simpler.
I would do it with PHP:
$text = preg_replace(
'#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#',
'$1', /* or Your TEXT */
$text
);
If I guess right, the url can be in your message, so your code should go like this:
<?php
echo
preg_replace(
'#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#smi',
'$1',
html_entity_decode($row11['message'])
);
?>

Categories