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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:04:04+00:00 2026-06-09T14:04:04+00:00

I am having trouble to separate the text. This is the scenario: $check =

  • 0

I am having trouble to separate the text.
This is the scenario:

$check = "Apple|Orange|Animal|Dog|Grape";

Suppose by using explode i could separate the word with “|”, but because the value i retrieved “Animal|Dog” should be a word so in this case, what would be the solution?? I could not use
limit as well because the position or number of text could be different.

The only way to distinctly separate the text is the Animal keyword. Is there any function in php that similar to mysql LIKE syntax?

If Case 2

$check = "Apple|Orange|Animal:Dog|Cat|Grape";
OR
$check = "Apple|Orange|Animal:Fish|Bird|Grape";

where the name of animal could be vary.
Output
"Apple|Orange|Animal:Dog,Cat|Grape" or "Apple|Orange|Animal:Fish,Bird|Grape"

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-06-09T14:04:06+00:00Added an answer on June 9, 2026 at 2:04 pm

    If all you want to do is replace “Animal|” with “Animal:” then you can do a simple str_replace:

    $check = "Apple|Orange|Animal|Dog|Grape";
    $newCheck = str_replace("Animal|","Animal:"); // will be set to 'Apple|Orange|Animal:Dog|Grape'
    

    Is that what you meant?

    EDIT, FOR CASE 2:

    I assume you have a string like "Apple|Orange|Animal:Dog|Cat|Grape", which has the category followed by 2 members of the category. From what you’ve said, you want to transform this string into "Apple|Orange|Animal:Dog,Cat|Grape" with a comma separating the two group members instead of a pipe. This is more complicated than the first case – the category name could vary, and you can’t do a simple str_replace starting with the colon because the first member of the group could vary as well. For this case, you’ll need to use a regular expression to match and replace the pattern of the string. Here’s the code:

    $check = "Apple|Orange|Animal:Dog|Cat|Grape";
    $newCheck = preg_replace("#(Animal:\w+)\|#", "$1,", $check); // will be set to "Apple|Orange|Animal:Dog,Cat|Grape"
    

    DEMO

    Let me explain what this does, in case you’re not familiar with regular expressions. The first argument of the preg_replace function, "#(Animal:\w+)\|#", tells PHP to look for all substrings of $check that begin with the text “Animal” followed by a colon, then a string of words with one or more character, and end with a pipe. This will look for the category name as well as the first member of that category in your string. The second argument, ":$1,", tells PHP to change the first pipe after this pattern into a comma. If you have a different category name, simply change the pattern you pass as the first argument to the preg_replace function:

    $check = "Apple|Orange|Animal1:Fish|Bird|Grape";
    $newCheck = preg_replace("#(Animal1:\w+)\|#", "$1,", $check); // will be set to "Apple|Orange|Animal1:Fish,Bird|Grape"
    

    Let me know if this is hard to follow!

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

Sidebar

Related Questions

I'm having trouble setting up and placing values into an array using a text
Having trouble framing this assertion in Rspec for my rails app. A User has
I'm having trouble taking data from a table and using it to replace strings
I am having trouble with the with mvc2. I am using the Entity Framework
I'm having some trouble getting my button text to align correctly in my Android
I'm having trouble trying to accomplishing this.. Whatever option I select from a dropdown
I'm having trouble determining a way to parse a given text file. Here is
I'm having trouble moving my functions into a separate file as they access a
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
having trouble coming up with search parameters for this issue, so I can't find

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.