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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:37:38+00:00 2026-06-17T09:37:38+00:00

I have a PHP variable that contains HTML. I need to check for occourances

  • 0

I have a PHP variable that contains HTML. I need to check for occourances of a string, and have that string replaced with the result of a function.

The function has not yet been written, but will only require one variable passed to it which will be the ID of the product.

For instance, the website has products, I want the user to be able to type something into the wysiwyg editor like {{product=68}} and it will display a preset div container with the information for that product. In this case the product has ID 68 in the database, but it could be anything from 1 to whatever.

I thought about creating an array of product id’s and searching for the first part of the string, but felt it may be rather cumbersome, I think one of our resident reg exp genius may be able to shed some light on what i need to do.

So the question is… how do i search a string for occourances of {{product=XXX}} where XXX can be an integer higher than one, capture that number, and pass it to a function which creates the replacement string which ultimatley replaces the occourance of the string ?

  • 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-17T09:37:38+00:00Added an answer on June 17, 2026 at 9:37 am

    Here is a regexp that matches {{product=##}} (doesn’t matter how many digits you enter):

    {{product=([0-9]+)}}
    

    Edit: Sorry, didn’t see you wanted it starting with 1:

    {{product=([1-9][0-9]*)}}
    

    If you want to capture the number, to it like this:

    $string = '{{product=68}}';
    preg_match_all( '%{{product=([1-9][0-9]*)}}%', $string, $matches );
    $number = (int) $matches[1][0];
    

    To give you a better understanding of preg_match_all, this is the content of $matches:

    array
      [0] => array // An array with strings that matched the whole regexp
        [0] => '{{product=68}}'
      [1] => array // An array with strings that were in the first "match group", indicated by paranthesis in the regexp
        [0] => '68'
    

    E.g. $matches would look like this if you had the string ‘{{product=68}}{{product=70}}’:

    array
      [0] => array
        [0] => '{{product=68}}'
        [1] => '{{product=70}}'
      [1] => array
        [0] => '68'
        [1] => '70'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that takes a variable number of arguments (using func_num_args()
I have a PHP-variable called $go_Adress which contains the adress I need to get
I have a variable, $var , that contains a string of characters, this is
I have a variable ( $page ) that contains HTML code as follows: <html>
Say i have a .html file that contains a web design and has variables
i have value in php variable like that $var='2.500000550'; echo $var what i want
I have this small PHP script that takes a POST variable and writes it
i have a variable $cartTotal in cart.php page i want to use that same
I have certain PHP class methods that access external variables. These variables are not
In my HTML I have a content div that contains articles created by a

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.