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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:43:28+00:00 2026-06-15T00:43:28+00:00

Possible Duplicate: Robust, Mature HTML Parser for PHP I’m trying to grab the first

  • 0

Possible Duplicate:
Robust, Mature HTML Parser for PHP

I’m trying to grab the first sentence of a string and the first image html instance.

$description = preg_split('/<img/', $item->description,null,PREG_SPLIT_DELIM_CAPTURE);

I’m able to returns an array but it’s removing the <img from it’s values which is needed. I’ve tried using flags but can’t get the return I’m looking for which need to include the delimiter itself. I know to grab the first sentence I should be able to split by period or &nbsp;

String:

<p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> <img alt="amj" src="https://domain.com/images7.jpg" /> <img alt="Ea" src="http://domain.com/images3.jpg" /> <img alt="amj" src="https://domain.com/images7.jpg" /> <img alt="amj" src="https://domain.com/images7.jpg" />
  • 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-15T00:43:29+00:00Added an answer on June 15, 2026 at 12:43 am

    If you make use of PREG_SPLIT_DELIM_CAPTURE you need to provide a capture within the regular expression pattern used with preg_split.

    In your current pattern:

    /<img/
    

    There is mothing to capture, that is why you see it removed (Demo):

    Array
    (
        [0] => <p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> 
        [1] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
        [2] =>  alt="Ea" src="http://domain.com/images3.jpg" /> 
        [3] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
        [4] =>  alt="amj" src="https://domain.com/images7.jpg" />
    )
    

    However, if you create a capture out of it, it will be captured:

    /(<img)/
    

    Result (Demo):

    Array
    (
        [0] => <p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> 
        [1] => <img
        [2] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
        [3] => <img
        [4] =>  alt="Ea" src="http://domain.com/images3.jpg" /> 
        [5] => <img
        [6] =>  alt="amj" src="https://domain.com/images7.jpg" /> 
        [7] => <img
        [8] =>  alt="amj" src="https://domain.com/images7.jpg" />
    )
    

    As you can see, preg_split does it’s documented job and will add another split per each capture of the first capturing supgroup (it will only take the first). You then might need to extend it across the full tag, which has been outline in different other html-like-string-regex questions, for example (limited as usual with regular expressions, so blame that you use preg_* functions instead of a HTML parser if you run into issues, not the pattern itself:

    /(<img [^>]*>)/
    

    Result (Demo):

    Array
    (
        [0] => <p>First sentence here comes.&nbsp; Second sentence here it is.&nbsp; One more sentence.&nbsp;&nbsp;</p> 
        [1] => <img alt="amj" src="https://domain.com/images7.jpg" />
        [2] =>  
        [3] => <img alt="Ea" src="http://domain.com/images3.jpg" />
        [4] =>  
        [5] => <img alt="amj" src="https://domain.com/images7.jpg" />
        [6] =>  
        [7] => <img alt="amj" src="https://domain.com/images7.jpg" />
        [8] => 
    )
    

    You would make your code more stable by using a standard HTML parser.

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

Sidebar

Related Questions

Possible Duplicate: Robust, Mature HTML Parser for PHP i have html file like <table
Possible Duplicate: How to parse and process HTML with PHP? I'm trying to scrape
Possible Duplicate: What is the fastest XML parser in PHP? I still need to
Possible Duplicate: Variable name as a string in Javascript What I'm trying to do
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How to successfully rewrite old mysql-php code with deprecated mysql_* functions? I
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always

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.