Deniz Zoeteman
Deniz Zoeteman

Reputation: 10111

Search for text in output and use whatever is behind the text

I was wondering how I could search for certain text (ie: description), and use all that's behind that text as a variable (all that is on the same line) in PHP. This needs to be read from an external location, a PHP file that must be ran (the PHP file reads content from a MySQL database and outputs this as a sort of index)

Any help would be appreciated.

Edit to clarify:

Sample input:

Lorum ipsum dolor

Sample output:

dolor

Upvotes: 1

Views: 102

Answers (2)

xzyfer
xzyfer

Reputation: 14135

Do you mean that given this string:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

$wordToFind = "cupidatat";

$allTextBehindWordToFind = substr($str, strpos($str, $wordToFind));

echo $allTextBehindWordToFind; 
// outputs: 'cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

Or:

$allTextBehindWordToFind = substr($str, strpos($str, $wordToFind) + strlen($wordToFind));

echo $allTextBehindWordToFind; 
// outputs: ' non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

Upvotes: 2

Lèse majesté
Lèse majesté

Reputation: 8045

Alternatively, you could do this:

$tail = array_pop(explode($needle, $string, 2));

But be sure to check $tail against the original string. If $needle wasn't found in $string, $tail will be $string.

Upvotes: 0

Related Questions