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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:59:19+00:00 2026-05-26T22:59:19+00:00

Generally I’d match HTML attributes with this regex \w+=.*? but when the HTML contains

  • 0

Generally I’d match HTML attributes with this regex

\w+=".*?"

but when the HTML contains PHP code it gets kind of dicy. Please consider the following tag:

<option value="<?php echo $img; ?>"<?php echo ($hpb[$i]['image_filename']==$img?' selected="selected"':''); ?>>
    <?php echo $img; ?>
</option>

the above regex will match the attribute selected="selected" which is determined inside PHP logic. Is there a way to match attributes which are not inside PHP tags while still matching the ones whose value may contain PHP logic? If not could I just remove the PHP code which isn’t part of an attribute value?

EDIT: Here’s what I have so far:

 \w+="(((.(?!<\?php))*?)|((.((?=<\?php).*?(?=\?>))*)*?))*"

Which basically means match a string which starts with a SPACE then greedily match alphanumeric characters followed by EQUALS sign followed by double quote and then match any of the following two while capturing as many characters as possible:

  1. A sequence of characters which does not contain the string <?php
  2. A sequence of characters containing the pattern <\?php.*?\?> or in other words greedily match the value part of the attribute with all of its PHP code
    All of that till a closing double quote is encountered…
  • 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-26T22:59:20+00:00Added an answer on May 26, 2026 at 10:59 pm
    /<\?php[\s\S]*?\?>|\s+(\w+)="([^"<]*(?:<\?php[\s\S]*?\?>[^<"]*)*)"/
    

    This will match either a PHP code segment or a complete attribute="value" sequence in which the value may contain PHP code. After each match you can find out what you caught by checking the contents of the capturing groups. If it’s a pure PHP segment you matched, all but group[0] will be empty; otherwise, group[1] will contain the attribute name and group[2] will contain the value.

    The regex assumes < will appear inside an attribute value only as the beginning of a <?php tag. Of course that’s not a syntactically valid assumption, but it’s probably safe anyway. I can make the regex more precise if you need me to, but it will be also be much less readable.

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

Sidebar

Related Questions

Generally I seem to be able to fix IE problems nowadays.. but this one
Generally code samples use locks this way: static readonly object lockForWorkingWithSharedObject = new object();
I generally use Subclipse, but there's some wonkiness in my code right now and
I generally do the following, but I'm hoping for a library that handles this
Generally I use lxml for my HTML parsing needs, but that isn't available on
Generally I work with CSV files but for this project I need to support
Generally, I would use boost::mpl::for_each<>() to traverse a boost::mpl::vector , but this requires a
Generally when I use ClickOnce when I build a VB.NET program but it has
Generally don't do OO-programming in Python. This project requires it and am running into
Generally this question comes from Eclipse suggesting to add a serial version UID on

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.