Forum Moderators: coopster

Message Too Old, No Replies

regular expression to find special HTML characters

“ and such

         

too much information

7:45 pm on Jun 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am trying to replace special characters in a block of text such as ’ ≴ and the like but for some reason I can only replace the text if it is by itself (separated with spaces) and this regular expression thing is driving me mad.

I know there is there a quick way to find these types of strings in a block such as:

google’s

using a regular expression that would take any character before and after the string without grabbing the surrounding text but I can't seem to get it.

Can someone point me in the right direction?

Receptional Andy

7:56 pm on Jun 8, 2008 (gmt 0)



any character before and after the string without grabbing the surrounding text

Can you clarify what it is you're trying to match? Perhaps give the data that you would like to obtain from the string you posted.

too much information

8:24 pm on Jun 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to eliminate those characters from my block of text, so instead of "the fox’s tail gave away his position" I want "the fox s tail gave away his position"

the str_replace and even ereg_replace isn't catching those character codes (or any number strings for that matter) unless they are standing alone in the block such as "this is the code for an apostrophe ’ it will show as a single character in your browser"

So I need to match &#--; without regard to what characters are before or after it in a string.

Receptional Andy

8:36 pm on Jun 8, 2008 (gmt 0)



So, something like:


$string="google’s";
// match &# followed by one or more numbers, then a semi-colon
$pattern="/(&#[0-9]+;)/";
// replace with a space
$string=preg_replace($pattern,' ',$string);
// show output
echo $string;

If the characters are not always numbers, you can match one or more characters that aren't a semi-colon:

$pattern="/(&#[^;]+;)/";

[edited by: Receptional_Andy at 8:37 pm (utc) on June 8, 2008]

too much information

4:17 am on Jun 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds so easy but I'm still not understanding the way these things are created. Is there a quick reference to this stuff somewhere?

That worked by the way, thanks for the help.

coopster

1:53 pm on Jun 9, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is there a quick reference to this stuff somewhere?

The PHP manual pages are often your best starting point for resources. The modifiers and syntax [php.net] descriptions can take some time to learn, but well worth your effort!