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 5957039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:20:38+00:00 2026-05-22T18:20:38+00:00

When I read the alt(technically title)-text of this XKCD comic , I became curious

  • 0

When I read the alt(technically title)-text of this XKCD comic, I became curious whether every articles in Wikipedia eventually points to Philosophy article. So I began to make a web application that displays what articles it’s “pointing” using PHP.

(PS: don’t worry about traffic – because I’ll use it privately and will not send too much requests to Wikipedia server)

To do this, I have to remove texts between parentheses and italics, and get the first link. Other things can be achieved using PHP Simple HTML DOM Parser, but remove texts between parentheses is the problem..

If there’s no parentheses in parentheses, then I could use this RegEx:\([^\)]+\), however, like the article about German language, there’s some articles have overlapped parentheses(for example: German (Deutsch [ˈdɔʏtʃ] ( listen)) is..), and above RegEx can’t handle these cases, since [^\)]*\) finds first closing parentheses, not matching closing parentheses. (Actually above case doesn’t become a problem since there’s no text between two closing parentheses, but it becomes a big problem when there’s a link between two closing parentheses.)

One dirty solution I can think is this:

$s="content of a wikipedia article";$depth=0;$s2="";
for($i=0;$i<strlen($s);$i++){
    $c=substr($s,$i,1);
    if($c=='(')$depth++;
    if($c==')'){if($depth>0)$depth--;continue;}
    if($depth==0) $s2.=$c;
}
$s=$s2;

However, I don’t like this solution since it cuts down a string into single characters and that looks like unnecessary…

Is there other ways to remove text in a pair of(matching) parentheses?

For example, I want to make this text:

blah(asdf(foo)bar(lol)asdf)blah

into this:

blahblah

but not like this:

blahbarasdf)blah

Edit : from a comment of Emil Vikström’s answer, I realized that above approach(remove texts between parentheses) may remove a link containing parentheses. However, I still want the answer of above problem, since I met similar problem before and I want to know the answer…

So my question is still: how to remove texts between matching parentheses?

  • 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-22T18:20:39+00:00Added an answer on May 22, 2026 at 6:20 pm

    Great! I am seeing someone with a problem which I experienced while cleaning up Wikipedia plain text content. Here is how you use it.

    cleanBraces("blah(asdf(foo)bar(lol)asdf)blah", "(", ")")
    

    will return

    blahblah

    You can pass any type of braces. Like [ and ] or { and }

    Here goes my source code.

    function cleanBraces($source, $oB, $eB) {
        $finalText = "";
        if (preg_match("/\\$oB.*\\$eB/", $source) > 0) {
            while (preg_match("/\\$oB.*\\$eB/", $source) > 0) {
                $brace = getBracesPos($source, $oB, $eB);
                $finalText .= substr($source, 0, $brace[0]);
                $source = substr($source, $brace[1] + 1, strlen($source) - $brace[1]);
            }
            $finalText .= $source;
        } else {
            $finalText = $source;
        }
        return $finalText;
    }
    
    function getBracesPos($source, $oB, $eB) {
        if (preg_match("/\\$oB.*\\$eB/", $source) > 0) {
            $open = 0;
            $length = strlen($source);
            for ($i = 0; $i < $length; $i++) {
                $currentChar = substr($source, $i, 1);
                if ($currentChar == $oB) {
                    $open++;
                    if ($open == 1) { // First open brace
                        $firstOpenBrace = $i;
                    }
                } else if ($currentChar == $eB) {
                    $open--;
                    if ($open == 0) { //time to wrap the roots
                        $lastCloseBrace = $i;
                        return array($firstOpenBrace, $lastCloseBrace);
                    }
                }
            } //for
        } //if
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this PHP RegEx page , but either I'm missing something, misreading something,
I read in this article that a company has created a software capable of
This question is related to the previous post. How to save file and read
Having read the threads Is SqlCommand.Dispose enough? and Closing and Disposing a WCF Service
I read somewhere once that the modulus operator is inefficient on small embedded devices
I read the Git manual, FAQ, Git - SVN crash course, etc. and they
Please read the whole question. I'm not looking for an approach to managing multi-lingual
I read an interesting DailyWTF post today, Out of All The Possible Answers... and
I read the following in a review of Knuth's The Art of Computer Programming:
I read some of the answers on here re: testing views and controllers, and

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.