Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get everything after word

Tags:

regex

php

Take this Lorem Ipsum text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla felis diam, mattis id elementum eget, ullamcorper et purus.

How can I with PHP and regex get everything that comes after Nulla?

like image 966
halliewuud Avatar asked Jul 02 '12 08:07

halliewuud


Video Answer


1 Answers

Hmm you don't want to use some simple things like :

$str = substr($lorem, strpos($lorem, 'Nulla'));

if you do not want to look for Nulla, but also for 'null' you might consider using stripos instead of strpos... This code will include Nulla in the returned value. If you want to exclude Nulla, you might want to add it's lentgh to the strpos value i.e

$str = substr($lorem, strpos($lorem, 'Nulla') + 5);

At last, if you need to have something a bit more generic, and as suggested @Francis :

$needle = 'Nulla'; 
$str = substr($lorem, strpos($lorem, $needle) + strlen($needle));

Honestly regexp are overkill for something like this...

like image 134
PEM Avatar answered Oct 12 '22 00:10

PEM