1

I am not good with preg_match or similar functions which are not deprecated.

Here are 2 strings:

  1. /Cluster-Computers-c-10.html

  2. /Mega-Clusters-c-15_32.html

I would to find out:

In number 1 example, how to get the value between -c- and .html (the value in the example is 10). The value is always an integer (numeric)

In number 2 example, how to get the value between -c- and .html (the value in the example is 15_32) . The value is always an integer seperated by _

Basically what I want to do is check if a string has either c-10.html or c-15_32.html and get the value and pass it to the database.

4 Answers 4

3

You can do:

preg_match('/-c-(\d+(?:_\d+)?)\.html$/i',$str);

Explanation:

-c-     : A literal -c-
(       : Beginning of capturing group
 \d+    : one or more digits, that is a number
 (?:    : Beginning of a non-capturing group
   _\d+ : _ followed by a number
 )      : End of non-capturing group
 ?      : Makes the last group optional
)       : End of capturing group
\.      : . is a metacharacter to match any char (expect newline) to match 
          a literal . you need to escape it.
html    : a literal html
$       : End anchor. Without it the above pattern will match any part 
          of the input string not just the end.

See it

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. I just wanted to confirm that if the url string has parameters such as mydomain.com/Cluster-Computers-c-10.html?action=blah
Also, just wanted to confirm that $matches will always have an array with 2 strings? or can it be more or less than 2 ever? I am asking because I need to assign $path = $matches[1]; and want to make sure it will always be same.
3
preg_match('~-c-(.*?)\.html$~', $str, $matches)
var_dump($matches);

3 Comments

Thanks for that. I just wanted to confirm that if the url string has parameters such as mydomain.com/Cluster-Computers-c-10.html?action=blah
Also, just wanted to confirm that $matches will always have an array with 2 strings? or can it be more or less than 2 ever? I am asking because I need to assign $path = $matches[1]; and want to make sure it will always be same.
@Eddy: it can be 0 or 2 items there. 0 - when nothing matched, 2 - when something matched. So put if (isset($matches[1])) { $path = ...; } else { nothing matched }
1
/-c-(\d+(?:_\d+)?)\.html$/i

-c- look for -c-
(\d+(?:_\d+)?) match number or number-underscore-number
\.html a period and trailing html
$ force it to match the end of the line
i case-insensitive match

Example:

<?php
  header('Content-Type: text/plain');
  $t = Array(
    '1) /Cluster-Computers-c-10.html',
    '2) /Mega-Clusters-c-15_32.html'
  );
  foreach ($t as $test){
    $_ = null;
    if (preg_match('/-c-(\d+(?:_\d+)?)\.html$/i',$test,$_))
      var_dump($_);
    echo "\r\n";
  }
?>

output:

array(2) {
  [0]=>
  string(10) "-c-10.html"
  [1]=>
  string(2) "10"
}

array(2) {
  [0]=>
  string(13) "-c-15_32.html"
  [1]=>
  string(5) "15_32"
}

Working Code: http://www.ideone.com/B70AQ

Comments

0

The simplest way I see would be:

preg_match( '/-c-([^.]+)\.html/i', $url, $matches );
var_dump( $matches );

1 Comment

. in the [] don't need to be escaped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.