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

  • SEARCH
  • Home
  • 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 8381013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:36:15+00:00 2026-06-09T16:36:15+00:00

I’m trying to explode a string by vertical bars. That’s the easy part. However,

  • 0

I’m trying to explode a string by vertical bars. That’s the easy part. However, I DON’T want the split to affect substrings that are surrounded by parentheses. That means I need a string such as:

Hello (sir|maam).|Hi there!

to explode into:

Array
(
    [0] => Hello (sir|maam).
    [1] => Hi there!
)

By using the normal explode function, I don’t believe there is a way to tell it to ignore that bar surrounded by the parentheses. However, I have some ideas.

I know that it would be possible to do this by exploding the string normally, and then looping through the array and merging everything between strings that contain ( to the closing string that contains ). However, I have a feeling that there should be a more elegant way of achieving this.

Am I right? Is there a less code-intensive means of spliting a string into an array given these restrictions?

  • 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-06-09T16:36:16+00:00Added an answer on June 9, 2026 at 4:36 pm

    If you can guarantee the parentheses will be balanced and never nested (that is, if there will never be a 'Oops(!' or a '(nested stuff (like this)|oops)'), and there will never be a || outside of parentheses that you care to match as an empty string, then this ought to help:

    preg_match_all('/(?:[^(|]|\([^)]*\))+/', $your_string, $matches);
    $parts = $matches[0];
    

    It’ll match [either (a character that’s not a | or (), or a ( and ) enclosing anything that’s not a ) (which includes |)], as many times as possible (but at least once). Short version: it’ll make | between parentheses part of the match, rather than a separator.

    Another possibility, that is slightly less cryptic:

    $parts = preg_split('/\|(?![^(]*\))/', $your_string);
    

    Uses a lookahead assertion to disqualify any | that’s followed by a ) if there’s not a ( in between. Still a bit unforgiving about parens, but it will match empty strings between two |s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.