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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:42:57+00:00 2026-05-16T22:42:57+00:00

I need to split (but not preg_split() ) my title on / , :

  • 0

I need to split (but not preg_split()) my title on /, : or whitespace, in that order. For example, if I split on /, then I don’t want to bother looking for : or whitespace.

I thought I could do it with a regex (the input string here will never have HTML in it)

$delimiters = array('/', ':', '\s');

$title = preg_replace('@(' . implode('|', $delimiters) . ')(.*)$@', '$1 <span>$2</span>', $title, 1);

The regex I have will match the first space, and not bother with the others. This is not what I want.

Obviously I could strpos() for the other characters (: and /) and remove the \s from the delimiters if it found the others. This will solve the first problem.

I also want to pick the furthest right match, i.e. if splitting a sentence on whitespace, I want the last word to be matched.

Do I need to use preg_split() here, and preserver the delimiter or can I do it with one regex?

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-16T22:42:58+00:00Added an answer on May 16, 2026 at 10:42 pm

    Well, here’s another one:

    function alexify($title) {
        $delimiters = array('/', ':', '\s');
    
        $regex = '@(' . implode('|', $delimiters) . ')([^' . implode($delimiters) . ']*)$@';
        $title = preg_replace($regex, '$1<span>$2</span>', $title, 1);
    
        return $title;
    }
    

    It appears to produce the same output as your function: http://ideone.com/HVeh5

    EDIT: It actually produces different output for the 3rd string. I don’t know which one is supposed to be the right one.

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

Sidebar

Related Questions

I need to split a string like this, on semicolons. But I don't want
I need to split a string by whitespace, but not when the whitespace is
I need split string by commas and spaces, but ignore the inside quotes, single
I need to use JavaScript split but I'm useless at regex so what i
I want to perform some copy actions on the build machine (but not on
i have string that i need to make custom parser but i am stuck
I need to split a string passing a regex, but the split token is
I need to split a string where there's a comma, but it depends where
I need to have a border line for the PdfPTable but not for the
I'm want to tokenize a text, but not separating only with whitespaces. There 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.