Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6231649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:58:19+00:00 2026-05-24T09:58:19+00:00

I am using the preg_split function in PHP in order to create one array

  • 0

I am using the preg_split function in PHP in order to create one array containing several different elements. However, I want to exclude a string which happens to contain one of the elements that I’m preg_splitting by.

$array['stuff'] = preg_split('/\[#]|\ &amp  |\ &amp |\&amp |\&amp|\ &amp|\ &gt  |\ &gt |\&gt |\&gt|\ &gt|\ &  |\ & |\& |\&|\ &|\ \/  |\ \/ |\\/ |\\/|\ \/|\ >  |\ > |\> |\>|\ >|\ ,  |\ , |\, |\,|\, |\ ::  |\ :: |\:: |\ ::|\::|\ ::|\ :  |\ : |\: |\:|\ :|\ -  |\ - |\- |\-|\ -/', $array['stuff'] ) ;

What I would like to do is to exclude a string such as ‘foo-bar’ from being matched for a split because it contains a dash. ‘foo-bar’ would need to be an exact match for my purposes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:58:20+00:00Added an answer on May 24, 2026 at 9:58 am

    The resulting regular expression would be very complicated specially if you have a lot exceptions like ‘foo-bar’.

    You should use a conditional subpattern with a lookbehind as condition and a lookahead as its yes-pattern:

    $res = preg_split('/(?(?<=foo)\-(?!bar)|\-)/', 'aasdf-fafsdf-foo-bar-asdf' );
    var_dump( $res );
    

    result:

    array(4) {
      [0]=>
      string(5) "aasdf"
      [1]=>
      string(6) "fafsdf"
      [2]=>
      string(7) "foo-bar"
      [3]=>
      string(4) "asdf"
    }
    

    Let me explain what is happening here. \- means

    Match any dash character.

    but what we want is

    Match any dash character that is not part of foo-bar.

    Since we can’t implement that in regex as it is we change it a little:

    Match any dash character that if preceded by foo is not followed by bar.

    To implement the if part we use a conditional subpattern, this is the syntax:

    (?(condition)yes-pattern|no-pattern)
    

    Our “condition” would be “preceded by foo” to check for that we use a lookbehind:

    (?<=foo)
    

    If that is true we should look for “a dash that is not followed by bar” to do that we use a negative lookahead:

    \-(?!bar)
    

    And that becomes our “yes-pattern”. Our “no-pattern” should be \- or “any dash”. The complete regex would be:

    (?(?<=foo)\-(?!bar)|\-)
    

    UPDATE: to incorporate this into your current regex change this part at the end:

    |\ -  |\ - |\- |\-|\ -/
    

    to

    |\s?(?(?<=foo)\-(?!bar)|\-)\s?/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to split some html content using php's preg-match-all function: <li class=cat-item><a title=blabla
i am using PHP scripts to implement this... $keyword=array('local news','art','local','world','tech','entertainment','news','tech','top stories','in the news','front page','bbc
I'm trying to split a string with text into words by using the php-function
I'm using php's preg_split to split up a string based on semi-colons, but I
I have an array which is being built using: $features_data[$i] = preg_split(/\n/, $row); The
One of my variable in my PHP script produce strings like: 'something/test/1' I want
I'm using preg_replace to create urls for modrewrite based paging links. I use: $nextURL
I am having an issue removing %3Cbr+%2F%3E from my string using the preg_replace function.
i'm able to split a string into an array $array = preg_split(/[\s]*[,][\s]*/, $category); RESULT
I wrote this function in php to check user/pass against account on linux server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.