Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all you want to do is replace "Animal|" with "Animal:" then you can do a simple <a href="http://php.net/manual/en/function.str-replace.php" rel="nofollow"><code>str_replace</code></a>:</p> <pre><code>$check = "Apple|Orange|Animal|Dog|Grape"; $newCheck = str_replace("Animal|","Animal:"); // will be set to 'Apple|Orange|Animal:Dog|Grape' </code></pre> <p>Is that what you meant?</p> <p><strong>EDIT, FOR CASE 2:</strong></p> <p>I assume you have a string like <code>"Apple|Orange|Animal:Dog|Cat|Grape"</code>, which has the category followed by 2 members of the category. From what you've said, you want to transform this string into <code>"Apple|Orange|Animal:Dog,Cat|Grape"</code> 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 <code>str_replace</code> 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:</p> <pre><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" </code></pre> <p><a href="http://sandbox.onlinephpfunctions.com/code/d2ae089f00d22851809660b0344d3c1148f5913f" rel="nofollow"><strong>DEMO</strong></a></p> <p>Let me explain what this does, in case you're not familiar with regular expressions. The first argument of the <a href="http://php.net/manual/en/function.preg-replace.php" rel="nofollow"><code>preg_replace</code></a> function, <code>"#(Animal:\w+)\|#"</code>, tells PHP to look for all substrings of <code>$check</code> 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, <code>":$1,"</code>, 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 <code>preg_replace</code> function:</p> <pre><code>$check = "Apple|Orange|Animal1:Fish|Bird|Grape"; $newCheck = preg_replace("#(Animal1:\w+)\|#", "$1,", $check); // will be set to "Apple|Orange|Animal1:Fish,Bird|Grape" </code></pre> <p>Let me know if this is hard to follow!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload