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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:09:01+00:00 2026-06-03T09:09:01+00:00

The middle of the HTML string viewed as text, after/before nearest sentence. Example: $str

  • 0

The middle of the HTML string viewed as text, after/before nearest sentence.

Example:

$str = 'First sentence. Second sentence! <a title="Hello. World">
        Third sentence. </a> Fourth sentence. Fifth sentence?';

$middle = strlen($str) / 2;

what I did is to append a keyword to all stop characters like ., ! or ?, but only to the ones outside HTML tags:

$str = 'First sentence. Second sentence! <a title="Hello. World">
        Third sentence. </a> Fourth sentence. Fifth sentence?';
$str = preg_replace_callback("/(\.|\?|\!)(?!([^<]+)?>)/i",  function($matches){
   return $matches[1].'<!-- stop -->';
}, $str);

This turns $str into:

First sentence.<!-- stop --> Second sentence!<!-- stop --> <a title="Hello. 
World"> Third sentence.<!-- stop --> </a> Fourth sentence.<!-- stop --> Fifth
sentence?<!-- stop -->

Now, how can I find the <!-- stop --> sub-string that’s nearest to the $middle position, and insert my text before it?

Is there a way I can find the position of $matches[1] inside my preg_replace callback relative to the whole string? That way I could just compare it with $middle

  • 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-03T09:09:03+00:00Added an answer on June 3, 2026 at 9:09 am
    <?php
        $str    = 'First sentence.<!-- stop --> Second sentence!<!-- stop --> <a title="Hello. 
        World"> Third sentence.<!-- stop --> </a> Fourth sentence.<!-- stop --> Fifth
        sentence?<!-- stop -->';
        $str1   =   explode('<!-- stop -->',$str);
        $str1[round(sizeof($str1)/2)-2].="!Append text!";
        $glue=htmlentities('<!-- stop --/>');
        echo $str2  =   implode($glue,$str1);
    ?>
    

    how is this?

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

Sidebar

Related Questions

If I had an html string containing this somewhere in the middle of it:
Is there any way to split a long string of HTML after N words?
I have the following html: <a href=javascript:<%# 'js code' %> class=MyClass> <div class=HeaderRow style=vertical-align:middle;text-align:left;height:35px;width:998px;>
If you go to http://www.wilwaldon.com/learning/inner4a.html with IE7 you will see the middle 2 columns
I have a long string in which there is some relevant text that i
In the given HTML markup below the middle row pink boxes overflow to the
This is how i configured the divs in HTML <div id=wrapper><div id=content><div id=details-middle class=box>
I'm looking for a way to split a string containing HTML in to two
I have a long text inside a div with defined width : HTML: <div>Stack
I have a string of HTML in Rails. I'd like to truncate the 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.