[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

PHP removing tags

Hello,

i have following problem ... i am sure its a simple solution but because i am fairly new to PHP i need a bit of guidance.

I want to remove tags that start with :

"<a href="Details.aspx?Tr=243029002"  (see below)  with string replace or some sort of similar function.

<div>
<a href="Details.aspx?Tr=243029002959224&">
SO51&nbsp;2000&nbsp;2900&nbsp;2909&nbsp;622
</a>
</div>
<div>
<a href="Details.aspx?Tr=290000002072265&">
SO51&nbsp;2000&nbsp;0000&nbsp;2002&nbsp;125
</a>
</div>


When i tried with this:

if (stripos($line, '<a href="Details.aspx?') !== false) {
   
                  $line = '';
         }

... i deleted the whole line including SO51&nbsp;2000&nbsp;2900&nbsp;2909&nbsp;622 which must remain...

Thank you for your help!
0
Caruso_eu
Asked:
Caruso_eu
1 Solution
 
Julian HansenCommented:
you can use preg_match (regular expression match) or strstr

if (strstr($line, '<a href="details.aspx"')) 
  $line = '';

Open in new window

or
if (preg_match('/^.*Details.aspx.*$/', $line) > 0)
  $line='';

Open in new window

The last will match all lines that contain the string Details.aspx - modify to restrict the search further if this is too broad.
0
 
gr8gonzoConsultantCommented:
This code will leave behind the contents of the tag and strip out the <a href....> and </a> tag that match.

<?php

// Contents
$str = '<div>
<a href="Details.aspx?Tr=243029002959224&">
SO51&nbsp;2000&nbsp;2900&nbsp;2909&nbsp;622
</a>
</div>
<div>
<a href="Details.aspx?Tr=290000002072265&">
SO51&nbsp;2000&nbsp;0000&nbsp;2002&nbsp;125
</a>
</div>';

// Replace
$trStartsWith = "243029002";
$str = preg_replace('@<a href="Details.aspx\?Tr='.$trStartsWith.'[^>]+>([^<]+)</a>@i','\1',$str);

// Display
echo $str;
?>

Open in new window

0
 
Caruso_euAuthor Commented:
Awesome!

gr8gonzo your solution worked as a charm!

Thank you for solving my problem!

@julianH. I could not run yours for some reason ... its not working.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now