Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace degree and phi symobols

Tags:

regex

php

I've tried in many ways to replace ONLY degree and (greek)Phi symbols. I've googled and did everything the articles said to do, it's driving me crazy. I can't use htmlentities() because I don't want to encode the html tags... just those two characters, I do this... it doesn't return an error, just replaces the entire string with nothing

preg_replace(array('/( )+/', '/\x{00B0}/u', '/\x{03A6}/u', '/\x{03D5}/u'), array(' ','°','Φ','Φ'), $str);

By The Way, the first replace just gets rid of extra white space and that works fantastic, but when I add in the other three (degree, capital Phi and lowercase Phi), the entire string gets replaced with white space.

like image 854
user2287474 Avatar asked May 13 '26 13:05

user2287474


1 Answers

It appears your code is working as expected

Live Demo

Example

Code

<?php

$string = 'hi ϕllip       it is 900°';

$output = preg_replace(array('/( )+/', '/\x{00B0}/u', '/\x{03A6}/u', '/\x{03D5}/u'), array(' ','&deg;','&Phi;','&Phi;'), $string );


    echo "this is the output:"  . $output;

Output

this is the output:hi &Phi;llip it is 900&deg;
like image 60
Ro Yo Mi Avatar answered May 16 '26 01:05

Ro Yo Mi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!