0

I'm looking for a regex to match a given URL which contains something like
"/unknownnumber/knownstring1-unknownstring.html"
in order to redirect it with PHP to other new url like
"/unknownnumber/knownstring2-unknownstring"
to keep Google indexed URLs active.

I have used next statement, but $do_match returns 0, so I'm doing something wrong...

Could someone help me with my regular expression?

$myURL = "/unknownnumber/knownstring1-unknownstring.html";
$do_match = preg_match('~"([0-9]+)/knownstring1-(.*?)$.html"~', $myURL, $matches);
2
  • So echo '<pre>';print_r($matches);echo'</pre>';exit; is empty? Commented Dec 28, 2012 at 7:47
  • Yes, it returns an empty array, and $do_match returns 0 Commented Dec 28, 2012 at 8:11

4 Answers 4

2

A $ is a end of line marker. You need to place it at the end of the regex and also to mean a literal . you need to escape it.

preg_match('~"([0-9]+)/knownstring1-(.*?)\.html"$~'....
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that advice, but it still doesn't return any matches... I've seen maybe my regex should be like $do_match = preg_match('~"/([0-9]+)/knownstring1-(.*?)$.html"~', $myURL, $matches); with the extra slash before the number, but it still doesn't work...
2

Dot (.), dash (-) are meta characters that have special meaning.

If 'unknownumber' is a numeric part of your string, then regexp pattern would looke like this:

$do_match = preg_match('/\/(\d+)\/knownstring1\-([^\.]+)\.html/', $input, $matches);

2 Comments

I've joint both answers (as yours gave me next message: Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash), so i used "$do_match = preg_match('~"\/(\d+)\/knownstring1\-([^\.]+)\.html/"$~', $myURL, $matches);" but... it still doesn't return any match... sorry..
Sorry, was in rush. I've fixed the pattern. It had missing preceding delimiter - forward slash in particular case. However, since you're using tildes as delimiter, the adopted pattern would look like this:$do_match = preg_match('~\/(\d+)\/knownstring1\-([^\.]+)\.html~', $input, $matches);
1
$myURL = "/unknownnumber/knownstring1-unknownstring.html";
if(preg_match('#"/(\d+)/knownstring1-(.*)\.html"#', $myURL, $matches))
    var_dump($matches);

outputs:

php > $exp = '#"/(\d+)/knownstring1-(.*)\.html"#';
php > $str = '"/23421/knownstring1-unknownstring.html"';
php > if(preg_match($exp, $str, $matches)) var_dump($matches); else echo 'nope' .     PHP_EOL;
array(3) {
  [0]=>
  string(40) ""/23421/knownstring1-unknownstring.html""
  [1]=>
  string(5) "23421"
  [2]=>
  string(13) "unknownstring"
}
php > 

Comments

0

Give this a try:

$myURL = "/123/knownstring1-unknownstring.html";
$do_matches = preg_match('#\/(\d+)\/(.*)\-(.*)\.html#', $myURL, $matches);
print_r($matches);

print_r output:

Array
(
    [0] => /123/knownstring1-unknownstring.html
    [1] => 123
    [2] => knownstring1
    [3] => unknownstring
)

Comments

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.