Hamed Ap Hamed Ap - 27 days ago 10
PHP Question

How to remove query string from "href" attribute of anchor using php?

i have this string :

$string = '<a href="/title/tt3110958/?ref_=nm_flmg_act_2" style="color:#666" >';


how to remove all word from
?
to
"
in my string .
i try this:

$result = preg_replace("/\?.+/", "", $string);


but its remove all word after
?
!!!

Answer

Use /\?[^\"]+/ like bellow:

<?php
$string = '<a href="/title/tt3110958/?ref_=nm_flmg_act_2" style="color:#666" >';
$result = preg_replace("/\?[^\"]+/", "", $string);
echo $result;
?>

Output : <a href="/title/tt3110958/" style="color:#666" >

Comments