By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,718 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,718 IT Pros & Developers. It's quick & easy.

preg_replace question - ASCII characters?

P: n/a
I need to capture referrers to a website and populate a page with links to the
referring pages.

How do I use preg_replace to search for and replace all ampersands ('&') with
the corresponding HTML special character ('&')?

$refurl = trim(getenv("HTTP_REFERER"));
$refhost = parse_url($refurl);
$refhost = preg_replace("\&\", "\&\", $refhost);

This code returns a warning and an error:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ...

Parse error: parse error, unexpected ';' in ...

Thanks in advance.

Feb 8 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
$refhost = preg_replace("\&\", "\&\", $refhost);

I think all I needed to do was use the right escape character:

$refhost = preg_replace("/&/", "/&/", $refhost);

still testing...

Feb 8 '07 #2

P: n/a
still testing...

testing failed. still looking for an answer.
Feb 8 '07 #3

P: n/a
On 8 Feb, 08:08, "deko" <d...@nospam.comwrote:
I need to capture referrers to a website and populate a page with links to the
referring pages.

How do I use preg_replace to search for and replace all ampersands ('&') with
the corresponding HTML special character ('&amp;')?

$refurl = trim(getenv("HTTP_REFERER"));
$refhost = parse_url($refurl);
$refhost = preg_replace("\&\", "\&amp;\", $refhost);

This code returns a warning and an error:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ...

Parse error: parse error, unexpected ';' in ...

Thanks in advance.
Why not just use str_replace

Feb 8 '07 #4

P: n/a
On 8 Feb, 10:25, "Captain Paralytic" <paul_laut...@yahoo.comwrote:
On 8 Feb, 08:08, "deko" <d...@nospam.comwrote:


I need to capture referrers to a website and populate a page with links to the
referring pages.
How do I use preg_replace to search for and replace all ampersands ('&') with
the corresponding HTML special character ('&')?
$refurl = trim(getenv("HTTP_REFERER"));
$refhost = parse_url($refurl);
$refhost = preg_replace("\&\", "\&\", $refhost);
This code returns a warning and an error:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ...
Parse error: parse error, unexpected ';' in ...
Thanks in advance.

Why not just use str_replace- Hide quoted text -

- Show quoted text -
Actually thinking about it further, why aren't you using
htmlentities() or htmlspecialchars()?

Feb 8 '07 #5

P: n/a
You should use the function htmlspecialchars().

$refhost = htmlspecialchars($refhost);

But even through you have changed it, you should just see "Array".
That's because of this line:
$refhost = parse_url($refurl);
Ok, so you wanted to get the host name, instead of just having
$refhost = htmlspecialchars($refhost);

you have to make it this:
$refhost = htmlspecialchars($refhost['scheme'] . '://' .
$refhost['host'] . (isset($refhost['port']) ? $refhost['port'] : ''));

Feb 8 '07 #6

P: n/a
You should use the function htmlspecialchars().

$refhost = htmlspecialchars($refhost);

But even through you have changed it, you should just see "Array".
That's because of this line:
$refhost = parse_url($refurl);
Ok, so you wanted to get the host name, instead of just having
$refhost = htmlspecialchars($refhost);

you have to make it this:
$refhost = htmlspecialchars($refhost['scheme'] . '://' .
$refhost['host'] . (isset($refhost['port']) ? $refhost['port'] : ''));

Feb 8 '07 #7

P: n/a
"deko" <de**@nospam.compíse v diskusním príspevku
news:RM******************************@comcast.com. ..
>I need to capture referrers to a website and populate a page with links to
the referring pages.

How do I use preg_replace to search for and replace all ampersands ('&')
with the corresponding HTML special character ('&amp;')?

$refurl = trim(getenv("HTTP_REFERER"));
$refhost = parse_url($refurl);
$refhost = preg_replace("\&\", "\&amp;\", $refhost);
$refhost = preg_replace('/\&/', '\&amp\;', $refhost);

Note - I use single quotes.

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Feb 8 '07 #8

P: n/a
"the DtTvB" <me********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
You should use the function htmlspecialchars().

$refhost = htmlspecialchars($refhost);

But even through you have changed it, you should just see "Array".
That's because of this line:
>$refhost = parse_url($refurl);

Ok, so you wanted to get the host name, instead of just having
$refhost = htmlspecialchars($refhost);

you have to make it this:
$refhost = htmlspecialchars($refhost['scheme'] . '://' .
$refhost['host'] . (isset($refhost['port']) ? $refhost['port'] : ''));
Thanks for the tip - htmspecialchars() is the way to go...

Here's more complete code:

$refurl = trim(getenv("HTTP_REFERER"));
$refurl = htmlspecialchars($refurl, ENT_NOQUOTES, 'ISO8859-1');
$refhost = parse_url($refurl);
$reflink = "<a href='".$refurl."'>".$refhost['host']."</a>";

what do you think?

still testing...

Feb 8 '07 #9

P: n/a
$refhost = preg_replace('/\&/', '\&amp\;', $refhost);

I think this will work, but htspecialchars() might be a better fit here.

Another situation where preg_replace() might be better is this:

Let's say I get a visitor using the following agent:

<a href="http://netforex.net"Forex Trading Network Organization </a>
in**@netforex.org

This indicates a robot (not a real visitor), so I want to identify it as such
with this code:

//well-known browsers
if(eregi("firefox.",$agent))
{
$browser = "firefox";
}
elseif(eregi("other browsers")
{
$browser = "other browsers";
}
//well-known bots
elseif (eregi("(bot|google|slurp|scooter|spider|infoseek|[etc., etc.])",
$agent))
{
$browser = "bot";
}
//unknown bots - identified by having a URL in the agent srting
elseif (eregi("http://", $agent))
{
$agent = stristr($agent, "http://");
$agent = parse_url($agent);
$agent = $agent['host'];
$agent_a = explode(".", $agent);
$agent_r = array_reverse($agent_a);
$sub = count($agent_r) - 1;
$tld3 = substr($agent_r[0], 0, 3);
if (eregi("^(com|net|org|edu|biz|gov)$", $tld3)) //common tld's
{
while ($sub 0)
{
$domain = $domain.$agent_r[$sub].".";
$sub--;
}
$refurl = $domain.$tld3; //use for building referring site link
}
$browser = "bot";
}
else
{
$browser = "unknown";
}

I know this is kind of a hack... any thoughts for improvement?
Feb 8 '07 #10

P: n/a
message <RM******************************@comcast.com>
"deko" <de**@nospam.comwrote:
>I need to capture referrers to a website and populate a page with links to the
referring pages.

How do I use preg_replace to search for and replace all ampersands ('&') with
the corresponding HTML special character ('&amp;')?

$refurl = trim(getenv("HTTP_REFERER"));
$refhost = parse_url($refurl);
$refhost = preg_replace("\&\", "\&amp;\", $refhost);
Why not use str_replace:

$refhost = str_replace('&','&amp;', $refhost);

[snip]
--
Kev Wells http://kevsoft.topcities.com
http://kevsoft.co.uk/
ICQ 238580561
Shine forth upon our clouded hills?
Feb 8 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.