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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:02:42+00:00 2026-06-11T13:02:42+00:00

i use preg_mach for extract some html ( i try to use DOMDocument but

  • 0

i use preg_mach for extract some html ( i try to use DOMDocument but i had some problem with new line )
any way … that’s my code ..

1.html

<body>


            <!-- icon and title -->
            <div class="smallfont">
                <img class="inlineimg" src="images/icons/icon1.gif" alt="" border="0" />
                <strong>qrtoobah 3nwan</strong>
            </div>
            <hr size="1" style="color:#CCCCCC; background-color:#CCCCCC" />
            <!-- / icon and title -->


        <div id="post_message_14142536">

            <font size="7"><font color="red">msaha 700</font></font><br />
<font size="7"><font color="red">shamali 20</font></font><br />
<font size="7"><font color="red"> 1700 almetr</font></font><br />
<font size="7"><font color="#ff0000">sooom bs</font></font><br />
<font size="7"><font color="#ff0000">albee3 qreeb</font></font>
        </div>
        <!-- message -->


</body>

extract.php

<?php 
$html = file_get_contents("1.html");
$pattern = '/<([!]+)([^]+).*>([^]+)(message\ \-\-\>)/';
   preg_match($pattern, $html, $matches);
 print_r($matches);


?>

i want to get any thing between <!-- icon and title -->)blablabla(<!-- / message -->…
but i get that array :

Array ( [0] => [1] => ! [2] => -- [3] => message --> ) 
  • 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-11T13:02:43+00:00Added an answer on June 11, 2026 at 1:02 pm

    Use strpos to find first tag position. Then find ending tag also with strpos. I mean – if u know from where to what you are looking for and they are unique.. so what matter in preg_* functions?

    So i guess something like this will work fine (I make code clear as possible for understanding my idea in step-by-step actions):

    $tag_begin = "<!-- icon and title -->";
    $tag_end   = "<!-- message -->";
    $begin     = strpos($tag_begin,$text)+strlen($tag_begin);
    $end       = strpos($tag_end,$text);
    $result    = substr($begin,$end, $text);
    

    Also u can do exactly the same if u want find and store all structures between opening <!-- (.*) --> and closing <!-- / (.*) -->.
    Only change u must do – first find with preg_match all opening structures names. For example:

    $result_cnt = preg_match_all('#<!-- [^/].*-->#', $text , $openings);
    
    // Output for your example HTML is:
    $openings = 
    array (
      0 => 
      array (
        0 => '<!-- icon and title -->',
        1 => '<!-- message -->',
      ),
    )
    

    After that one-loop for $openings and find with code above all needed. just adding to openings closing “/” chacter in right place.

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

Sidebar

Related Questions

I've got some html string and I'd like to use preg_replace to replace everything
I know I can use preg_match but I was wondering if php had a
Is there any way to use preg_match or any other similar smarty function to
Im tring to use preg_match to grab image URLs from another page but problem
For some reason when I use preg_replace() for a input field that stores phone
I always use preg_match and it always works fine, but today I was trying
I'm trying to use preg_match to extract info from href=domain.com/subdir/?key=value The info I want
I am trying to use preg_replace function to parse this table in a Html
I'm trying to use preg_match to find a commented part in a loaded HTML
I'm beginner with PHP and I try to use preg_math_all to split a string.

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.