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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:21:21+00:00 2026-05-15T17:21:21+00:00

I need a function or some regex to split up spaces in a string

  • 0

I need a function or some regex to split up spaces in a string but to treat an HTML tag as a word.

$str = 'one two <a href="">three</a> four';
$x = explode(" ", $str);
print_r($x);

/* Returns:
  Array
(
    [0] => one
    [1] => two
    [2] => <a
    [3] => href="">three</a>
    [4] => four
)

Looking for way to return:

Array
(
    [0] => one
    [1] => two
    [2] => <a href="">three</a>
    [3] => four
)

*/

Any ideas? Thanks

  • 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-15T17:21:22+00:00Added an answer on May 15, 2026 at 5:21 pm

    This is a bit simpler then the above, haven’t fully tested but give it a shot.

    $str = 'one two <a href="">three</a> four';
    
    if(preg_match_all('%(<[^<]+.*?>|[^\s]+)%', $str, $matches)) {
        array_shift($matches);
        print_r($matches);
    }
    

    Here is another version that I tested for about 5 minutes that works a bit better:

    $str = 'one two <a href="omfg hi I have spaces"> three</a> four <script type="javascript"> var a = "hello"; </script><random tag>la la la la<nested>hello?</nested></random tag>';
    
    if(preg_match_all('%(<[^<]+.*?>|[^\s]+)%', preg_replace('%([\s]\<|\>[\s])%', '$1', $str), $matches)) {
        array_shift($matches);
        echo '<pre>';
        print_r($matches);
        echo '</pre>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function to change the appearance of some elements in my HTML
How to change HTML background with JavaScript Function? I need some simple function to
regular = 'a string'; enriched = enrichString(regular); sys.puts(enriched); function enrichString(str){ //run str through some
need some help from a regex jedi master: If I have a string of
I know what is the function of the bash commands but i need some
I need to remove some html markup that occurs inside an img tag. This
I need some function 'apply_patch': result_string = apply_patch('origin_file.txt', 'origin_file.txt.patch') or some commands (for windows
I have some function where I need to pass a point datatype . somefunc(United
Need to call a filter function on some options based on a radio selected
i'm a beginner to php. i need to use php function which process some

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.