Raj
Raj

Reputation: 1437

Check if a word exist in a url slug using php

My url slug is like below

/page-1/block-1/subpage

Now I want to search the slug for the word 'block'

I used stripos but it didn't give me the right output.

$pos2 = stripos("block", $row->uri);
if ($pos2 == true) {
    echo "We found block in '$row->uri' at position $pos2";
}

$pos2 is always false here.

Any help is highly welcomed. Thanks in advance.

Upvotes: 0

Views: 884

Answers (3)

Alexander Kerchum
Alexander Kerchum

Reputation: 532

You have your haystack and needle in the wrong order. The thing you are searching through goes first.

<?php

  $pos2 = stripos('/page-1/block-1/subpage', "block");
  if ($pos2) {
    echo "We found block in '/page-1/block-1/subpage' at position $pos2";
}

Upvotes: 1

baao
baao

Reputation: 73221

if (stripos($row->uri,'block')) {
echo "We found block in '$row->uri' at position ".stripos($row->uri,'block');}

Just to make it as short as possible, and in the right order (haystack and needle).

Upvotes: 1

Victory
Victory

Reputation: 5890

$pos2 = stripos($row->uri, "block"); your needle and haystack are reversed. See official docs

Upvotes: 3

Related Questions