1

Suppose I have a string like this:

Ch #RoKamb93 ajesop de Bourg #kin #kinnl 9 kunsho taffetas,bunughs rges, note florale,parfaite aseref

My array of delimiters can be :

array('#kin', '#kinnl', '#xxx' );

How can Iparse it to get the Ch #RoKamb93 ajesop de Bourg substring ? Substring just before #kin OR #kinnl

Note: Both #kin OR #kinnl may not present at the same time.

1
  • 2
    Can you please be more specific on the rules by which you want to parse the string? Also ... whathaveyoutried.com Commented Sep 5, 2013 at 10:38

1 Answer 1

1

How about this:

$string = 'Ch #RoKamb93 ajesop de Bourg #kin #kinnl 9 kunsho taffetas,bunughs rges, note florale,parfaite aseref';

$delimiters = array('#kin', '#kinnl', '#xxx');
$pattern = '/' . implode($delimiters, '|') . '/';

$split = preg_split($pattern, $string);
var_dump($split); // $split[0] has what you need

If your delimiters contain reserved regex characters (like . or ?), you'll need to do something like this for the pattern:

$pattern = '/' . implode(array_map('preg_quote', $delimiters), '|') . '/';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.