unidox Posted August 4, 2008 Share Posted August 4, 2008 I am trying to figure on how to do this: What I need to do is search for <a href></a> tags, but only replace & inside the href="" and leave the rest. See, the problem is I am merging php and html together, then using eval() to put it all together, I tried to use htmlspecialchars but it replaces some the of the php code. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/ Share on other sites More sharing options...
centerwork Posted August 4, 2008 Share Posted August 4, 2008 What is the trigger, purpose, or both for changing out the href=""? Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607067 Share on other sites More sharing options...
unidox Posted August 4, 2008 Author Share Posted August 4, 2008 what Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607085 Share on other sites More sharing options...
unidox Posted August 4, 2008 Author Share Posted August 4, 2008 What I am trying to do is replace all & with & but only if its in a <a> tag. Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607157 Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 Admittedly, sometimes it helps to show a literal example of a before and after sample.. so for example, this snippet: $test = '<a href="www.test-site.com">This is a test</a>'; echo $test . '<br />'; $test = preg_replace('#(?<=(<a href="))(.*)(?=(">))#', 'www.replace.com', $test); echo $test; will result in having what looks like 2 identical looking links.. but if you right-click and view source code, you will see this: <a href="www.test-site.com">This is a test</ a> and this: <a href="www.replace.com">This is a test</ a> You can play around with the 'source' (text to be replaced) and the 'target' (text that will replace) in the preg expression. Hope this helps.. Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607210 Share on other sites More sharing options...
unidox Posted August 4, 2008 Author Share Posted August 4, 2008 Well all I am trying to do is replace & with &. The problem is, in php code I use && for AND in both sql and if statements. Can I work around this? Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607215 Share on other sites More sharing options...
unidox Posted August 4, 2008 Author Share Posted August 4, 2008 I tried this: function amp($content) { $content = preg_replace("/\s&&\s/", "AND", $content); $content = preg_replace("@(.+)&(.+)@", "&", $content); return $content; } but it still wont replace the & with &. Whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607250 Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 I tried this: function amp($content) { $content = preg_replace("/\s&&\s/", "AND", $content); $content = preg_replace("@(.+)&(.+)@", "&", $content); return $content; } but it still wont replace the & with &. Whats wrong? Ok.. dude.. I'm so close (I don't know if I got it or not).. but I chose a different route.. here's what I came up with (and I'll use your function format): Let's assume that your variable $content = '< a href="www.test-site.com?prime=1&oct=6&log=10&dual=2">This & is a test</ a>'; (I added spaces to not confuse the post settings...) function amp($content) { $x = preg_split('#"#', $content, -1, PREG_SPLIT_NO_EMPTY); // isolate the stuff in quotes by breaking $content into an array by splitting using quotes... $x[1] = preg_replace('#&#', htmlspecialchars('&') , $x[1]); // $x[1] is the array segement that has everything in quotes.. and I replace & with & // echo $x[1]; $new = implode('"', $x); // re-attach the array back ($new).. echo $new; // this should put a link onscreen.. but you can also return it.. return ($new); } So here's the thing.. if I echo out $x[1] in the line after I replaced & with & , it seems to have worked perfectly.. but (yes, there is a but...), when the link is pasted on screen, and I rightclick and view source code, this is what I find.. <a href="www.test-site.com?prime=1&oct=6&log=10&dual=2">This & is a test</a> I cannot understand the extra (amp;) parts.. but as I said before, when I printed out $x[1], it seems to work perfectly.. so perhaps it is something I did in the implode() functionality? In either case, I think I have taken this as far as I can run in my limited knowledge.. but I'll get emails on any posts in this thread.. so keep my updated on what you ultimately come up with..(I'm curious to know myself).. Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607324 Share on other sites More sharing options...
sasa Posted August 4, 2008 Share Posted August 4, 2008 try <?php echo htmlspecialchars('&'); // result is: & ?> Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607356 Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 try <?php echo htmlspecialchars('&'); // result is: & ?> This is what is confusing me.. onscreen, the actual echoed result is simply & When I right-click and view source, it has & Any reason why this is the case? Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-607706 Share on other sites More sharing options...
sasa Posted August 5, 2008 Share Posted August 5, 2008 & browser interpret as & and after that is show amp; Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-608554 Share on other sites More sharing options...
KevinM1 Posted August 5, 2008 Share Posted August 5, 2008 Well all I am trying to do is replace & with &. The problem is, in php code I use && for AND in both sql and if statements. Can I work around this? If this is for output, like you stated in the JavaScript board, then don't worry about overwriting your PHP code with & Judging by your request, and the obvious difficulty you're having executing such a simple task, it leads me to believe your overall application design is lacking. Like all programming ventures, logical flow and clarity is key. A well-formed PHP application will look like this: PHP: Handle user requests. Scrub/verify/secure incoming data. [1] Query the DB to save/retrieve results. Form and format the output. Store dynamic output (including any JavaScript that's dependant on PHP logic) in variables. Include() static output (like a <div> that acts like a footer on each page). [2] | | | V HTML: Display page structure in a logical manner. Avoid including PHP code within the page structure. Avoid inline JavaScript. So, in your case, you should run htmlEntities() at either point [1], if the output you're concerned about is actually written by a user, or [2] if, for some reason, you decide to keep the '&' characters inside the database but want to transform them before outputting them. In all honesty, your problem shouldn't be more difficult to solve than: if(isset($_POST['submit'])) { if(isset($_POST['description'])) { $desc = mysql_real_escape_string(addslashes(htmlentities(striptags($_POST['description'])))); /* this strips HTML tags from the input, then converts all questionable characters into their HTML entity counterparts, then adds slashes in case magic quotes is turned on (may be redundant because of htmlentities), then, finally, it escapes the input making it ready for storage in the DB */ } } Quote Link to comment https://forums.phpfreaks.com/topic/118005-solved-replace/#findComment-608631 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.