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
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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.