1

Please read the code below and comments to see what I'm trying to do. Its hard to explain in a paragraph.

$url_fixx = 'home/sublink/group-view/product/catview1'; 
// What my variable holds. MY GOAL IS: I want to omit group-view, product and catview1 from this so I use the trick below. 

catview has a random number at the end so I use the code below to find the number at the end and it outputs "catview1" in this case

$string = $url_fixx;
$matches = array();
if (preg_match('#(catview\d+)$#', $string, $matches)) {
    $catViewCatch = ($matches[1]);
 }
// I got this from http://stackoverflow.com/a/1450969/1567428

$url_fixx = str_replace( array( 'group-view/', 'product', 'catview1' ), '', $url_fixx );
// this outputs what I want. 

MY QUESTION IS :

//When I replace "catview1" with $catViewCatch, the whole str_replace doesnt work. 
$url_fixx = str_replace( array( 'group-view/', 'product', $catViewCatch), '', $url_fixx );

Why is that? and what am I doing wrong?

PS: Also my url sometimes changes to something like this.
$url_fixx = 'home/sublink/group-view/anotuer-sublink/123-article'

How can I tackle all these ?

2
  • What does $catViewCatch contain when you capture it using the preg_match call? Also, if your URL is always 5 levels like this, why not just explode on the / character, and slice off the first 2 elements of the array? Commented Sep 5, 2012 at 1:03
  • @JonahBishop $catViewCatch contains "catview1" when i capture it using preg_match. I cant use explode because the url can change. Sometimes $url_fixx = 'home/sublink/group-view/anotuer-sublink/123-article'; Commented Sep 5, 2012 at 1:20

1 Answer 1

3

Both of your examples output the exact same thing. The below code demonstrates this:

<?php
$url_fixx = 'home/sublink/group-view/product/catview1';

$string = $url_fixx;
$matches = array();
if (preg_match('#(catview\d+)$#', $string, $matches)) {
    $catViewCatch = ($matches[1]);
}
echo str_replace( array( 'group-view/', 'product', 'catview1' ), '', $url_fixx );
echo '<br />';
echo str_replace( array( 'group-view/', 'product', $catViewCatch), '', $url_fixx );
?>

Also, you might consider using preg_replace instead as it will accomplish the task with less code:

echo preg_replace('#group-view/product/catview[0-9]+#','',$url_fixx);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I commented on Jonah's reply saying my url can change. What If if my url changes like this $url_fixx = 'home/sublink/group-view/anotuer-sublink/123-article'
How do you want your example URL to look? I'm having trouble understanding the format to give you a good answer.
All I want to do is remove all the "group-view" "product" and "catview[number]" values from my url. Thats it.
You might consider using str_replace() for that but to stick to the one line solution you could use the following: echo preg_replace('#(group-view/)|(product/)|(catview[0-9]+)#','',$url_fixx);

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.