Halter
Halter

Reputation: 48

Strstr alternatives

The strstr() function searches for the first occurrence of a string inside another string. My question is, is there an alternative to strstr that searches for all occurrences?

Upvotes: 0

Views: 2122

Answers (3)

Michel Feldheim
Michel Feldheim

Reputation: 18250

You can use preg_match_all it is looking for all occurrences matching a regular expression pattern.

This will work slightly other than strstr http://php.net/manual/en/function.preg-match-all.php

Depending on how exactly you are using strstr you'll define your expression.

If required, I can help you with this once the exact use is clear

Upvotes: 1

Ali Mohammadi
Ali Mohammadi

Reputation: 1324

<?php
// get last directory in $PATH
$dir = substr(strrchr($PATH, ":"), 1);

// get everything after last newline
$text = "Line 1\nLine 2\nLine 3";
$last = substr(strrchr($text, 10), 1 );
?>

Upvotes: 0

Ray Paseur
Ray Paseur

Reputation: 2194

http://php.net/manual/en/function.substr-count.php might do it for you.

These are added bytes to make SO accept a succinct answer.

Upvotes: 3

Related Questions