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.

Help storing pattern-match in variable?

Featured Replies

I have a pattern, a string I'd like to search through, and I want the matching section of the string to be stored in a variable.

 

Pattern: [0-9]*(?=\.gif$)
String: http://figmint.zzl.org/photos/3.gif

 

I tested this in a regex tester and it highlighted the "3", which is what I want. I've never done this before though, so I don't know what function I should be using for this, or how to go about it.

 

 

 

 

Also, I was trying to test this on my own with an if/else, and it always outputs "no match" for some reason:

<?php

if (preg_match("[0-9]*(?=\.gif$)", "http://figmint.zzl.org/photos/3.gif")) {
echo "it matched";
}
else {
echo "no match";
}

?>

Pattern: [0-9]*(?=\.gif$)
String: http://figmint.zzl.org/photos/3.gif

 

I tested this in a regex tester and it highlighted the "3", which is what I want. I've never done this before though, so I don't know what function I should be using for this, or how to go about it.

 

Hi there, it seems you haven't included your regular expression delimiters, this would be the reason the code functioned for you in a regex tester, however under a server environment you are getting the following:

 

Warning: preg_match() [function.preg-match]: Unknown modifier '*' in C:\Desktop\noname1.php5 on line 3
no match 

 

Thus despite regex delimiters not being required (they are stripped when processed) adding them will resolve your issue.

 

<?php 

if (preg_match("#[0-9]*(?=\.gif$)#i", "http://figmint.zzl.org/photos/3.gif")) { 
echo "it matched"; 
} 
else { 
echo "no match"; 
} 

?>

 

These delimiters are simply a starting "#" and a finishing "#i" on the expression.

 

Hope that helps :)

Edited by CSN-UK

  • Author

I've got it working. Thanks!

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.