doddsey_65 Posted November 8, 2010 Share Posted November 8, 2010 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? Quote Link to comment Share on other sites More sharing options...
mikecampbell Posted November 9, 2010 Share Posted November 9, 2010 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); Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted November 9, 2010 Share Posted November 9, 2010 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 Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted November 9, 2010 Author Share Posted November 9, 2010 thanks for the info. But how would i add the url into a variable so i could call it in the popup box? Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted November 9, 2010 Share Posted November 9, 2010 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 Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted November 9, 2010 Author Share Posted November 9, 2010 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>'; Quote Link to comment Share on other sites More sharing options...
Buddski Posted November 9, 2010 Share Posted November 9, 2010 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); Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted November 9, 2010 Author Share Posted November 9, 2010 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 Quote Link to comment Share on other sites More sharing options...
Buddski Posted November 9, 2010 Share Posted November 9, 2010 Im not exactly what popup you are referring to? Can you show us an example of this popup? Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted November 9, 2010 Author Share Posted November 9, 2010 onclick="return confirm(\'You are being redirected to \\1. Proceed?\') That is a javascript alert box with confirm/cancel properties Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted November 9, 2010 Share Posted November 9, 2010 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; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.