Jump to content

preg replace


doddsey_65

Recommended Posts

basically what i am trying to do is replace the

 

<a href="whatever.com"> 

 

with

 

<a href="whatever.com" onclick = "if (! confirm("Continue?")) return false;">

 

so that when the user clicks the url in a message the box pops up first then redirects them to the url if they click confirm. However i am getting the following error:

 

Warning: preg_replace() [function.preg-replace]: Compilation failed: reference to non-existent subpattern at offset 11

 

here is the code i used.

 

$message_content = $content_info['message_content'];
$pattern='<a href="\\1">';
$replace='<a href="\\1" onclick = "if (! confirm("Continue?")) return false;">';
$message_content = preg_replace($pattern, $replace, $message_content);
echo $message_content;

 

Anyone know where i went wrong?

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/
Share on other sites

You need to use brackets to designate subpatterns. You also need to put / around the pattern (or another matching pair of characters).

 

$pattern='/<a href="([^\"]*)">/';
$replace='<a href="\\1" onclick = "if (! confirm(\'Continue?\')) return false;">';
$message_content = preg_replace($pattern, $replace, $message_content);

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132015
Share on other sites

I didnt had much time but if it were just for an input value this works (with 1 string that is) substr might be nice instead of the heavier regex.

 

     $subject = '<a href="whatever.com">'; //could be anything $_POST['value'];
      $clean = substr($subject, 0,-1);
      $clean.= ' onclick = "if (! confirm("Continue?")) return false;">';
      echo $clean;// ofcours you wont see it, but if you check your source you do need to end it with </a> btw

 

-edit: i would go for preg though hehe

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132016
Share on other sites

I am not sure if I understand you right, but one thing I thought might be of your interest.

I am pretty sure your syntax is not right:

<a href="whatever.com" onclick = "if (! confirm("Continue?")) return false;">

maybe try:

 <a href="whatever.com" onclick="return confirm('continue?')">lalala</a>

 

To add php and using the script that was given i think you can just add it like this:

 

$pattern='/<a href="([^\"]*)">/';
        $replace='<a href="\\1" onclick = "if (! confirm(\''.$YOUR_VARIABLE_INSIDE_HERE.'\')) return false;">link</a>';
        $message_content = preg_replace($pattern, $replace, $message_content);

But again i am pretty sure that on-click part is not correct. If not try:

 

$pattern='/<a href="([^\"]*)">/';
        $replace='<a href="\\1" onclick="return confirm(\''.$YOUR_VARIABLE_INSIDE_HERE.'\'));">link</a>';
        $message_content = preg_replace($pattern, $replace, $message_content);

 

-edit: I added:  link</a>  other wise you have nothing to click on

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132072
Share on other sites

I am not sure if I understand you right, but one thing I thought might be of your interest.

I am pretty sure your syntax is not right:

<a href="whatever.com" onclick = "if (! confirm("Continue?")) return false;">

maybe try:

 <a href="whatever.com" onclick="return confirm('continue?')">lalala</a>

 

To add php and using the script that was given i think you can just add it like this:

 

$pattern='/<a href="([^\"]*)">/';
        $replace='<a href="\\1" onclick = "if (! confirm(\''.$YOUR_VARIABLE_INSIDE_HERE.'\')) return false;">link</a>';
        $message_content = preg_replace($pattern, $replace, $message_content);

But again i am pretty sure that on-click part is not correct. If not try:

 

$pattern='/<a href="([^\"]*)">/';
        $replace='<a href="\\1" onclick="return confirm(\''.$YOUR_VARIABLE_INSIDE_HERE.'\'));">link</a>';
        $message_content = preg_replace($pattern, $replace, $message_content);

 

-edit: I added:  link</a>  other wise you have nothing to click on

 

What I mean is $YOUR_VARIABLE_INSIDE_HERE needs to be the url they are clicking. The href.

 

Eg/

 

$replace='<a href="\\1" onclick="return confirm(\'You are being redirected to '.$YOUR_VARIABLE_INSIDE_HERE.'\'));">link</a>';

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132121
Share on other sites

Is this what you are after? If correct you just need to put the 'match' into the JavaScript

 

$pattern='/<a href="([^\"]*)">/';
$replace='<a href="\\1" onclick="return confirm(\'You are being redirected to \\1. Proceed?\')">';
$message_content = preg_replace($pattern, $replace, $message_content);

 

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132132
Share on other sites

Is this what you are after? If correct you just need to put the 'match' into the JavaScript

 

$pattern='/<a href="([^\"]*)">/';
$replace='<a href="\\1" onclick="return confirm(\'You are being redirected to \\1. Proceed?\')">';
$message_content = preg_replace($pattern, $replace, $message_content);

 

thats what i am after and i did try it but the popup just echos \\1 it doesnt print the actual url

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132134
Share on other sites

Are you echoing $message_content?  I.e, this works:

 

<?
$message_content = 'This is a test: <a href="test.php">Test</a>';
$pattern='/<a href="([^\"]*)">/';
$replace='<a href="\\1" onclick="return confirm(\'You are being redirected to \\1. Proceed?\')">';
$message_content = preg_replace($pattern, $replace, $message_content);
echo $message_content;
?>

Link to comment
https://forums.phpfreaks.com/topic/218149-preg-replace/#findComment-1132411
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.