Jump to content
Sign in to follow this  
techboy999

preg_replace UTF-8

Recommended Posts

Hello guys

I sitting here and try to get a string in to links, I am almost there just problem I have is that some special characters will not link to the link

I need get the the preg_replace to use UTF-8, I know it's possible with a parameter /u, but I cant find out where to place it

 

Here is the code:

$str = $message4;

// Url Template
$template = '<a href="search.php?q=%1$s">%1$s</a>';

// Replace Words
echo preg_replace("/(?!(?:[^<]+>|[^>]+<\/a>))\b([a-å]+)\b/is", sprintf($template, "\\1"), $str);

Share this post


Link to post
Share on other sites

Before that,

[a-å]+
will not work: character ranges work on bytes (or Unicode codepoints) and that range will cover far more than just letters.

 

So you're trying to link all words. I'd go for

[\pL']+
to match letters and apostrophes.

 

As for getting Unicode mode, that's the /u flag. "Flags" go after the ending delimiter - you're using two of them already...

Share this post


Link to post
Share on other sites

Hi

 

It works almost with the [\pL']+ but I have 2 words that always not link the special characters the words are and and also også, you have any idea to fix that ?

Thanks in advance

Share this post


Link to post
Share on other sites

It should work fine. Are you sure the text is in UTF-8 encoding? What does bin2hex($str) show?

Share this post


Link to post
Share on other sites

?476f646d6f7267656e2048e56265722064752068617220736f76657420676f6474206f67206472f86d74204e6f676c652046616e7461736b7469736b65206472f86d6d652c2048617220647520696b6b652065742062696c6c656465206a6567206de52073652073f86465206f67206876616420657220646974206e61766e206f672068766f7220657220647520667261203f

 

some cryptic

Edited by techboy999

Share this post


Link to post
Share on other sites

That's not UTF-8. Probably ISO 8859-1 but I can't tell for sure right now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

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.