Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Regex help!

Featured Replies

Could someone please tell me if this is possible...

 

Basically what I have is:

preg_replace('/[^0-9ml]/', '',

What I want, it is to cut out everything except "20ml" for example. But the above code leaves every number and letter in as opposed to the "20ml".

 

Am I far away with that above?

 

Much appreciated...


My regex isn't great and I won't claim to be great at it; what you used is not too far off but what I think you're looking for is preg_match() or preg_match_all()

 

Something like this should work how you would like:

 

preg_match_all('/[0-9]{1,}ml/', 'lorem ipsum 20ml d0lor sit 2013 3ml', $matches);

print_r($matches);

 

$matches will hold an array of returned values, so you will end up with:

array(
[0] => '20ml',
[1] => '3ml'
);

 

 

I'll break it apart for you:

 

[0-9] is looking for any digit between zero and nine.

{1,} is asking the digit to be at least one digit in length.

ml is obviously looking for "ml"

 

With those combined it's matching the string as a whole.

 

And so you know where you were going wrong, here's what your regex was actually seeking and replacing:

 

[] Square brackets donate seeking for any character held within.

^ here you're saying "I want anything except the following characters..."

0-9ml the character and digit set you're seeking.

 

So, everything is not treated as a whole but as individuals because it resides in square brackets. And as you can see, you were actually looking to replace your desired characters/digits and not other characters.

 

Hope this helps.

Edited by Ampersandwich

  • Author

Thanks for the quick reply. After I posted that last night I had a feeling it might of been preg_match I was after.

 

I tried the above but still can't get it to work. I should probably of been a bit more precise with my code.

 

Basically I originally had:

<?php echo $row['description']; ?>

Which obviously displays the description from the database.

 

But all I want is "20ml", or whatever that part of the discription is.

 

So I tried the code in my last post which was actually:

<?php $ro = preg_replace('/[^0-9ml]/', '', $row['description']); 
echo $ro; ?>

So will your code still work in that situation?

 

Thanks again.

Edited by Lee_K

Yeah the code should work in that situation, if you use it like:

 

<?php
preg_match_all('/[0-9]{1,}ml/', $row['description'], $matches);

echo $matches[0]; // [0] is going to retrieve the first result from the array.
?>

 

What issues were you having with the code I posted?

  • Author

All I seem to get is 'array' echo'd out?

 

[EDIT]: I changed 'echo $matches[0]; to: 'print_r($matches);' which gives me something like:

'Array([0] => Array([0] => 25ml))'

So I feel like i'm getting there, just need to remove the "Array" part so i'm only left with "25ml".

 

Thanks for your help! I'm a total beginner when it comes to anything like this! So it's much appreciated!

Edited by Lee_K

Don't worry about it man. What you have there is a multidimensional array. Essentially that is an array (or arrays) within any given array. You can access these by following the path of the arrays.

 

Arrays can be broken up into two parts as such:

array(
    KEY => VALUE
)

 

The two parts are KEY and VALUE. The key represents the access points of the array and value is the information that the array holds. So the method of accessing the value would be as:

 

$var[KEY]

If we were to break apart a multidimensional array we would get the following:

array(
    KEY => array(
        KEY2 => VALUE
    )
)

 

So, as you can see here the inset array is actually the value of the first array. To get the inset value we can string together array keys by following the path KEY -> KEY2:

 

$var[KEY][KEY2];

This means if we take a look at your output, we can access "25ml" by doing:

echo $matches[0][0];

 

I hope that makes sense, I have a difficult time explaining things like this in full sometimes.

 

[EDIT]

I'd also recommend that you read up on arrays and their variations to familiarise yourself with them, as you will find yourself using them a lot in PHP. It's a vital part of the language.

Edited by Ampersandwich

  • Author

I messed around for ages trying to get that working, yet something as simple as adding an extra '[0]' was enough to fix it!

 

I also managed to tweak this quite a bit and finally got what I wanted.

 

There's something else i'm going to try but I think I can manage on my own (I hope). But if not i'll be back! :_

 

Thanks again, much appreciated... You said at the start that your regex isn't great, but I think your better than you give yourself credit for.

Edited by Lee_K

Sometimes things like that can be a bit frustrating, especially when you're starting out. Just keep at it :)

 

And no problem, I'm glad that you have it working now!

  • Author

Another quick question regarding preg_match_all.

 

Is it possible to match from more than one row? For example I have:

<?php preg_match_all('', $row['name'], $matches

But what i'm trying to do is something like this:

<?php preg_match_all('', $row['name'] $row['secondname'], $matches

Is that possible? Basically so that if it can't be found in the first row, to try the next row. I've been looking all night for something but had no luck in finding anything...

Edited by Lee_K

You could try to concatenate them, so they become one string:

<?php preg_match_all('', $row['name'] . $row['secondname'], $matches);

Notice the dot between the variables.

 

Léon

  • Author

Thanks, I think that fixed it! But i've got one more question, should be the last as well hopefully!

 

Basically with this:

preg_match_all('/[0-9]{1,}ml/', $row['description'], $matches);

I've noticed that in some of my rows, 'ml' is directly followed with another sentence, rather than as an individual word. Basically without any spaces in between. Usually something like this:

 

ml/3.4oz

Is there any way I can alter the code above so that it will cut off the remaining text?

/\d+ml\/\d+\.?\d*oz/

 

This should be what you're looking for. i haven't quite got the time to explain how this one works at the moment but I'll update this post in a few hours with another rundown

  • Author

Thanks! I think I understand how each part of it works actually. But what I want is to cut off everything after the ml. Not display it.

Basically some of the rows have "30 ml/1oz". So my original code wouldn't show these rows. So I only want to select everything up to "ml" and nothing after it.

Does that make sense?

 

[EDIT]: I should also add that "/1oz" is just an example. It could possibly change. So really i'd need something that will only select the ml part of a full word or sentence that follows "[0-9]{1,}[\s]?"

Edited by Lee_K

  • Author

I think I've possibly made a mistake somewhere, and the original code works fine? It's actually selecting the rows that contain "ml/1oz" and only showing the ml part. Like I want it too... Apologies if this is the case!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.