Prajila V P
Prajila V P

Reputation: 5327

Is there any function similar to stristr() in PHP 5.2

Is there any function similar to stristr()? I want to use stristr(), but I can't because my PHP version is 5.2.9.

So I need a similar function which gives the same functionality.

<?php
$email = '[email protected]';
echo stristr($email, 'e'); // outputs [email protected]
echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US
?>

How can i do this?

Upvotes: 0

Views: 419

Answers (2)

symcbean
symcbean

Reputation: 48357

If you want to use the 'before needle' functionality, this is trivial to implement yourself using the 2 parameter version....

 function stristr_bn($haystack, $needle)
 {
    $post=stristr($haystack, $needle);
    if ($post===false) return false;
    return substr($haystack, 0, strlen($haystack)-strlen($post)-strlen($needle));
 }

However this a very messy solution to the problem of parsing an ADRR_SPEC (regardless of implementation).

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75598

Use stripos and substr:

echo substr($email, 0, stripos($email, 'e'));

Upvotes: 3

Related Questions