Jump to content

[SOLVED] Replace


unidox

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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&amp;oct=6&amp;log=10&amp;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

Link to comment
Share on other sites

try

<?php
echo htmlspecialchars('&');
// result is: &amp;
?>

 

This is what is confusing me.. onscreen, the actual echoed result is simply &

When I right-click and view source, it has &amp;

 

Any reason why this is the case?

 

Cheers,

 

NRG

Link to comment
Share on other sites

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 */
   }
}

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.