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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:11:21+00:00 2026-05-10T15:11:21+00:00

I’m having an issue with my regex. I want to capture <% some stuff

  • 0

I’m having an issue with my regex.

I want to capture <% some stuff %> and i need what’s inside the <% and the %>

This regex works quite well for that.

$matches = preg_split('/<%[\s]*(.*?)[\s]*%>/i',$markup,-1,(PREG_SPLIT_NO_EMPTY  |  PREG_SPLIT_DELIM_CAPTURE)); 

I also want to catch &amp;% some stuff %&amp;gt; so I need to capture <% or &amp;lt;% and %> or %&amp;gt; respectively.

If I put in a second set of parens, it makes preg_split function differently (because as you can see from the flag, I’m trying to capture what’s inside the parens.

Preferably, it would only match &amp;lt; to &amp;gt; and < to > as well, but that’s not completely necessary

EDIT: The SUBJECT may contain multiple matches, and I need all of them

  • 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. 2026-05-10T15:11:21+00:00Added an answer on May 10, 2026 at 3:11 pm

    In your case, it’s better to use preg_match with its additional parameter and parenthesis:

    preg_match('#((?:<|&lt;)%)([\s]*(?:[^ø]*)[\s]*?)(%(?:>|&gt;))#i',$markup, $out); print_r($out);  Array (     [0] => <% your stuff %>     [1] => <%     [2] => your stuff     [3] => %> ) 

    By the way, check this online tool to debug PHP regexp, it’s so useful !

    http://regex.larsolavtorvik.com/

    EDIT : I hacked the regexp a bit so it’s faster. Tested it, it works 🙂

    Now let’s explain all that stuff :

    • preg_match will store everything he captures in the var passed as third param (here $out)
    • if preg_match matches something, it will be store in $out[0]
    • anything that is inside () but not (?:) in the pattern will be stored in $out

    The patten in details :

    #((?:<|&lt;)%)([\s]*(?:[^ø]*)[\s]*?)(%(?:>|&gt;))#i can be viewed as ((?:<|&lt;)%) + ([\s]*(?:[^ø]*)[\s]*?) + (%(?:>|&gt;)).  ((?:<|&lt;)%) is capturing < or &lt; then % (%(?:>|&gt;)) is capturing % then < or &gt;  ([\s]*(?:[^ø]*)[\s]*?) means 0 or more spaces, then 0 or more times anything that is not the ø symbol, the 0 or more spaces. 

    Why do we use [^ø] instead of . ? It’s because . is very time consuming, the regexp engine will check among all the existing characters. [^ø] just check if the char is not ø. Nobody uses ø, it’s an international money symbol, but if you care, you can replace it by chr(7) wich is the shell bell char that’s obviously will never be typed in a web page.

    EDIT2 : I just read your edit about capturing all the matches. In that case, you´ll use preg_match_all the same way.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer As we can give a name to a method, that… May 11, 2026 at 1:36 pm
  • added an answer Regarding other answers here, it is not a good idea… May 11, 2026 at 1:36 pm
  • added an answer Yeah, continuations can be pretty mind-bending. Here's a good puzzle… May 11, 2026 at 1:36 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.