Jump to content

shed light on preg_replace, please


DamienRoche

Recommended Posts

I'm totally confused here.

 

I have this:

$string = "numbers = 21,200";
$number = preg_replace("/[^0-9]/", "", $string);

This outputs: 21200

 

But I just don't get it. My understanding is that it is saying - replace all 0-9 with nothing and use string as the subject.

 

does the ^ indicate everything but?

 

Finally, can anyone help me to not filter out the comma?

 

As always, any help or advice is very much appreciated. Thanks.

Link to comment
Share on other sites

Fantastic...these regex's are numbing my mind.

 

I do have another question.

 

I know you can use \b to indicate an exact word but how do I use it with the ^ anything but I just discovered.

 

Example (won't work):

<?php
$string = "numbers = 21,200";
$number = preg_replace("/[^0-9\,|\bnumb\b]/", "", $string);
echo $number;
?>

 

or:

 

<?php
$string = "numbers = 21,200";
$number = preg_replace("/[^0-9\,]|\bnumb\b/", "", $string);
echo $number;
?>

 

It just keeps pulling numb from numbers.

 

Thanks again.

Link to comment
Share on other sites

 

Finally, can anyone help me to not filter out the comma?

 

As always, any help or advice is very much appreciated. Thanks.

 

if you don't want to filter out the comma, add it inside the range

$number = preg_replace("/[^0-9,]/", "", $string);

 

or else, if the string is distinct, just explode it, no need for regexp

$a=explode(" ",$string);
echo end($a);

Link to comment
Share on other sites

<?php
$string = "numbers = 21,200";
$number = preg_replace("/[^0-9\,]|\bnumb\b/", "", $string);
echo $number;
?>

 

This pattern is not logical. You are telling regex to replace anything that is NOT 0-9 or a comma (you don't need to escape the comma), -or- the word num with nothingness (""). Since this is an alternation, the first condition is met, thus everything that is not a number nor a comma will be wiped out.

In other words, if there is a comma or a number in your string, this is the ONLY condition that will ever be met.

 

As for your \bnum\b part, this will never work when checked against 'number'. Text nested within the \b...\b characters are surrounded by non-word characters... you cannot use \b...\b to find a part of a word. It matches a complete word. So in this case, your pattern is searching for a word by itself spelled num.

 

My strong advice is to read up on regex here to get a better understanding.

Constantly 'shooting in the dark' is not the best way to learn. Read up on regex material, then experiment.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.