473,394 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

preg_replace question - ASCII characters?

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
10 9689
$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
still testing...

testing failed. still looking for an answer.
Feb 8 '07 #3
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
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
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
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
"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
"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
$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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sebastian Araya | last post by:
Hello, I have a string like this: var1: value1...valueI var2: value1...valueJ ... varN: value1...valueK this is an example: breakfast: coffee eggs lunch: sandwich apple dinner: chicken...
4
by: Sidharta | last post by:
Hi all, how come this doesn't work????? # convert to unix new lines $text = preg_replace("/\r\n/", "\n", $text); # remove extra new lines $text = preg_replace("/\n+/", "\n", $text); is...
1
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the...
37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
5
by: julianmlp | last post by:
I'm fairly new to regex code, and I'm trying to find a way to replace any text after "///" (three slashes) and before a "\n" character. I've tried with: $filen = preg_replace("///./\n$", "",...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.