Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted characters in a string

I would like to ask how to remove a special character from a string(extracted from a scrapped page).

 4:30am

I just want to get the time so ive tried so filter it using this:

$str = 'Â 4:30am';
$new_string = preg_replace("[^A-Za-z0-9]", "", $str); 
echo '<pre>'.$new_string.'</pre>';

But it doesn't change :| Is there any solution/approach?

like image 712
Vainglory07 Avatar asked Nov 18 '25 01:11

Vainglory07


2 Answers

Your regex is invalid...

$str = 'Â 4:30am';
$new_string = preg_replace("~[^a-z0-9:]~i", "", $str); 
echo '<pre>'.$new_string.'</pre>';

... and you forgot ":" in the regex, so in your case it will be removed.

like image 159
Glavić Avatar answered Nov 20 '25 15:11

Glavić


Your regex pattern needs to be enclosed by delimiters. Your current pattern is using the [ and ] as the delimiters, which most likely isn't what you intended to do.

preg_replace("/[^A-Za-z0-9]/", "", $str); 

http://pl.php.net/manual/en/regexp.reference.delimiters.php

like image 25
Supericy Avatar answered Nov 20 '25 14:11

Supericy



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!