php - regular expression to extract foo, bar, baz and logical operator

992

String Parsing

foo_and_bar_and_baz_or_doe

expected result: foo, bar, baz and doe along with and, and and or.

i tried/(.*?)(_and_|_or_)/i but its skipping last one.

actually sting is

having_tag_slugs_and_having_category1_slug_and_having_category2_slug_or_having_category3_slug

out put, i am looking is

having_tag_slugs
having_category1_slug
having_category2_slug
having_category3_slug

_and_
_and_
_or_
''
987

Answer

Solution:

You can try the following regular expression -

/(.*?)(_and_|_or_|$)/im

Hope that helps. Let me know how it goes :)

890

Answer

Solution:

$a="foo_and_bar_and_baz_or_doe";
$c=explode("_",$a);
print_r($c);
337

Answer

Solution:

How about preg_split:

$str = 'having_tag_slugs_and_having_category1_slug_and_having_category2_slug_or_having_category3_slug';
$arr = preg_split('/(_and_|_or_)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($arr);

output:

Array
(
    [0] => having_tag_slugs
    [1] => _and_
    [2] => having_category1_slug
    [3] => _and_
    [4] => having_category2_slug
    [5] => _or_
    [6] => having_category3_slug
)

People are also looking for solutions to the problem: php - Zend - plugin Word_CamelCaseToDash was not found in the registry

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.