0

I have htacess rule like this:

RewriteRule ^([A-z])([0-9]+)-([^/]*)?$ index.php?tt=$1&ii=$2&ll=$3

Is there any PHP function that can do the same ?
Something like:

$A = XXXX_preg_match("([A-z])([0-9]+)-([^/]*)" , "A123-boooooo");
// $A become to =array("A","123","boooooo")
2

3 Answers 3

1
preg_match('/([a-zA-Z])(\d+)-([^\/]+)/', 'A123-boooooo', $A);
array_shift($A);

Output: print_r($A);

Array
(
    [0] => A
    [1] => 123
    [2] => boooooo
)
Sign up to request clarification or add additional context in comments.

1 Comment

You are missing the anchors from the original regex.
1

If you just want to retrieve those three values, you can pass an out-parameter to preg_match like this:

preg_match(
    '~^([A-z])([0-9]+)-([^/]*)$~' ,
    'A123-boooooo',
    $matches
);

$fullMatch = $matches[0]; // 'A123-boooooo'
$letter = $matches[1];    // 'A'
$number = $matches[2];    // '123'
$word = $matches[3];      // 'boooooo'

// Therefore
$A = array_slice($matches, 1);

If you want to do the replacement right away, use preg_replace:

$newString = preg_replace(
    '~^([A-z])([0-9]+)-([^/]*)$~',
    'index.php?tt=$1&ii=$2&ll=$3',
    'A123-boooooo
);

The documentation of these is usually really good for further information.

Comments

0

According to preg_match doc

preg_match("~([A-z])([0-9]+)-([^/]*)~" , "A123-boooooo", $matches);
print_r($matches);

output:

Array
(
    [0] => A123-boooooo
    [1] => A
    [2] => 123
    [3] => boooooo
)

1 Comment

You are missing the anchors from the original regex.

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.