php regex doesn't seem to work as expected

php regex cheat sheet
regular expression in php w3school
php regex tester
php regex extract
php regex special characters
php regex d+
php match string
regular expression validation in php



$url = '';
$out = [];

preg_match('\/\d{4}\/\d{2}\/\d{2}(.*)', $url, $out);

// At this point $out is empty... 

// Also...  I tried this (separately)

$keywords = preg_split("\/\d{4}\/\d{2}\/\d{2}(.*)", $url);
// also $keywords is empty... 

I've tested the regex externally and it works. I want to split out the /the-string-i-want-to-get/ string. What am I doing wrong?

I would not use a regex. In this case it's better to use parse_url and some other helpers like trim and explode.

$url = '';

$parsed = parse_url($url);
$Xploded = explode('/',trim($parsed['path'],'/'));
print $Xploded[count($Xploded)-1];

// outputs: the-string-i-want-to-get

preg_match - Manual, The syntax doesn't always seems straightforward at first, but once you learn it, In PHP every regular expression pattern is defined as a string using the Perl  office At its most basic level, a regex can be considered a method of pattern matching or matching patterns within a string. In PHP the most oft used is PCRE or “Perl Compatible Regular Expressions”. Here we will try to decipher the meaningless hieroglyphics and set you on your way to a powerful tool for use in your applications.

There's a function for that:

echo basename($url);

Learn Regular Expressions - PHP, Regular Expression is a compact way of describing a string pattern that matches a We cannot cover everything under this topic, but let us look into some of the  As I guessed, strpos() is always faster (about 2x) for short strings like a URL but for very long strings of several paragraphs (e.g. a block of XML) when the string doesn't start with the needle preg_match as twice as fast as strpos() as it doesn't scan the entire string.

preg_split Split string by a regular expression. Split the given string by a regular expression.

Your $url will be split by the dates. That's not the way you need to do:

  $url = '';
  $out = [];
  preg_match('/\/\d{4}\/\d{2}\/\d{2}(.*)/', $url, $out); 
  // See here...

You will get an array of two elements:

array(2) {
  string(37) "/2017/07/01/the-string-i-want-to-get/"
  string(26) "/the-string-i-want-to-get/"

Getting Started with PHP Regular Expressions, PHP - Regular Expressions - Regular expressions are nothing more than a sequence or pattern of characters itself. They provide the foundation for  As of PHP 5.3.0, the POSIX Regex extension is deprecated. There are a number of differences between POSIX regex and PCRE regex. This page lists the most notable ones that are necessary to know when converting to PCRE. The PCRE functions require that the pattern is enclosed by delimiters.

PHP, PHP REGULAR EXPRESSION also known as regex are powerful contains guru"; } else { echo "the url $my_url does not contain guru"; } ?>. This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given str. It uses the same tag stripping state machine as the fgetss () function. You can use the optional second parameter to specify tags which should not be stripped. HTML comments and PHP tags are also stripped.

PHP - Regular Expressions, PHP Regular Expressions PHP (version 5.3 and above) supports Perl style regular expressions via its preg_ family of functions. Function, What it Does  When preg_replace() is called with the /e modifier, the interpreter must parse the replacement string into PHP code once for every replacement made, while preg_replace_callback() uses a function that only needs to be parsed once.

PHP Regular Expressions Tutorial: Preg_match, Preg_split , For instance, to match http://, do you really want your regex string to look like '/http​:\/\//' ? Doesn't '~http://~' look better? Rare characters such as "~", "%", "#" or  Test PHP regular expressions live in your browser and generate sample code for preg_match, preg_match_all, preg_replace, preg_grep, and preg_split! — A Live Regular Expression Tester for PHP Tutorials and Regex Database NEW!

  • $keywords = preg_split("\/\d{4}\/\d{2}\/\d{2}(.*)", $out); Swap variable?
  • So how is it to work on the $url?
  • No no. You have to do differently.
  • You missed regex delimiters, use preg_match('\/\d{4}\/\d{2}\/\d{2}(.*)', $url, $out);
  • should be preg_match('/\/\d{4}\/\d{2}\/\d{2}(.*)/', $url, $out); I'm surprised no warning thrown
  • It's not even necessary to initiate $out as [] - also surrounding .* in paren not necessary
  • @JasonGenX Would you like to improve my answer. I don't understand it fully.