preg replace

General discussions related to php

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Thu Aug 02, 2012 3:25 am

preg replace

Postby WangLi » Thu Aug 02, 2012 3:32 am

Hello, i'm trying to write a simple script to replace all ascii codes in a string into their char value. Here's the code i wrote:

Code: Select all

$string = " I&#39;m a string ";
$pattern = "/&#(\d\d);/";
$replacement = chr("$1");
echo preg_replace($pattern, $replacement, $string); 

I think that in the replacement slot is not possible to use chr("$1") even if:
1) with $replacement = chr(39) preg_replace correctly returns the apostrophe
2) with $replacement = $1 preg_replace correctly returns "39"

how can i solve it ?

Thanks for you help

Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 4 guests