NYCPHP Meetup

NYPHP.org

[nycphp-talk] including an & in a variable

David Sklar sklar at sklar.com
Thu Jan 9 15:44:11 EST 2003


Yes.

Although I suppose if you had your content in a difficult-to-edit format, or
had thousands and thousands of links, you could do something like:

<?php

ob_start();

/* print out giant page */

$page = ob_get_contents();
ob_end_clean();

$page =
preg_replace('/?redir=([^"])+?"/e',"'?redir='.urlencode('\\\\1').'"',$page);

print $page;

?>

This captures the page into $page using output buffering and then encodes
the URLs with preg_replace() and urlencode. It assumes that there are no
single or double quotes in the URLs that need to be encoding. You should
doublecheck my preg_replace() regex syntax -- I haven't tested this code.

-dave


> -----Original Message-----
> From: Jaz-Michael King [mailto:JMKing at ipro.org]
> Sent: Thursday, January 09, 2003 3:32 PM
> To: talk at nyphp.org; sklar at sklar.com
> Subject: RE: [nycphp-talk] including an & in a variable
>
>
> So right now I have to point to dozens of pages like thi sone:
> http://www.medicare.gov/NHCompare/Search/HomeSelect.asp?State=NY1%
> 7CNew+York+-+Upstate&Type=County&County=33060%7CChautauqua
>
> So I use
>
> href="http://www.ipro.org/redirect.php?redir=http://www.medicare.g
ov/NHCompare/Search/HomeSelect.asp?State=NY1%7CNew+York+->
+Upstate&Type=County&County=33060%7CChautauqua"
>
> which counts the click and forwards the user (except for stopping
> at "&Upstate")
>
> but you're saying now I would need
> <?
> $redir =
> 'http://www.medicare.gov/NHCompare/Search/HomeSelect.asp?State=NY1
> %7CNew+York+-+Upstate&Type=County&County=33060%7CChautauqua'
> ?>
> ...
> href="http://www.ipro.org/redirect.php?redir=<?= urlencode($redir) ?>"
> ...
>
> for each link?
>
>
> >>> sklar at sklar.com 01/09/03 03:02PM >>>
> Use urlencode() to encode the URLs being passed to the link tracker.
>
> E.g. do this:
>
> $redir = 'http://www.domain.com?state=ny&county=kings';
> print '<a href="'.urlencode($redir).'">click here</a>.';
>
> Instead of this:
>
> $redir = 'http://www.domain.com?state=ny&county=kings';
> print '<a href="'.$redir.'">click here</a>.';
>
>
> -dave
>
>
> > -----Original Message-----
> > From: Jaz-Michael King [mailto:JMKing at ipro.org]
> > Sent: Thursday, January 09, 2003 2:39 PM
> > To: NYPHP Talk
> > Subject: [nycphp-talk] including an & in a variable
> >
> >
> > hi all
> >
> > I'm trying to pass a bunch of long urls to a script as a link tracker
> > but the urls contain ampersands. so, if I want to track the link
> > www.domain.com?state=ny&county=kings
> >
> > my script stops at the ampersand - I assume I have to escape it somehow?
> >
> > if (!isset($redir))
> >     die("Sorry, that link is bad. Please go back and try a
> > different link.");
> >
> > if (!isset($referrer)) {
> >     if (!isset($HTTP_REFERER)) {
> >         $referrer = "none";
> >     } else {
> >         $referrer = $HTTP_REFERER;
> >     };
> > };
> >
> > if (!isset($REMOTE_ADDR)) {
> >     $remotehost = "no client address";
> > } else {
> >     $remotehost = $REMOTE_ADDR;
> > };
> >
> > $timeused = date("Y-m-d H:i:s",time());
> >
> > @mysql_db_query($db_data , "insert into $db_table
> > values('$redir','$referrer',
> >     '$remotehost','$timeused')");
> >
> > Header("Location: $redir");
> > exit;
> >
> > thanks much,
> > jaz
> >
> > ******************************
> > Jaz-Michael King
> > Online Services Manager
> > IPRO
> > http://ipro.org
> > ******************************
> >
> >
> >
> >
> >
> >
> >
>
>
> --- Unsubscribe at http://nyphp.org/list/ ---
>
>




More information about the talk mailing list