tags:

views:

1146

answers:

1

Hi,

I want to take a string in PHP and discard everything after a certain character. However, it needs to search for not just one character, but an array of them. As soon as it gets to one of the characters in the array, it should return the string before that point.

For instance, if I have an array:

$chars = array("a", "b", "c");

How would I go through the following string...

log dog hat bat

...and end up with:

log dog h

Any solutions would be very much appreciated. :)

+6  A: 

The strcspn function is what you are looking for.

<?php

$mask = "abc";

$string = "log dog hat bat";

$result = substr($string,0,strcspn($string,$mask));

var_dump($result);

?>
Vinko Vrsalovic
Excellent, thank you!
Philip Morton