0

I'm trying to replace my strings with values in array by array key:

Example 1:

Array
(
    [cid] => 1-category-title
    [slug] => 100-article-title
)

My string: products/{:cid}/{:slug}

And I'm trying to replace it: products/1-category-title/100-article-title

Example 2:

Array
(
    [cid] => 1-category-title
    [slug] => 100-article-title
    [page_id] => 5
)

My string: products/{:cid}/{:slug}

And I'm trying to replace it: products/1-category-title/100-article-title?page_id=5

Example 3:

Array
(
    [cid] => 1-category-title
    [slug] => 100-article-title
    [page_id] => 5
    [format] => json 
)

My string: products/{:cid}/{:slug}

And I'm trying to replace it: products/1-category-title/100-article-title?page_id=5&format=json

1
  • Do you have a framework? Commented Dec 11, 2015 at 21:22

1 Answer 1

1

Here's an example.

Read this.

$array = array(
    "cid"=>"1-category-title",
    "slug"=> "100-article-title",
    "page_id" => 5
);

$string = "products/{:cid}/{:slug}";

$string = str_replace("{:cid}",$array['cid'], $string);
$string = str_replace("{:slug}",$array['slug'], $string);
if(!empty($array['page_id'])) $string .= "?page_id=".$array['page_id'];

echo $string;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.