Jump to content

[SOLVED] Splitting a string into one or more arrays


jonathandoe

Recommended Posts

Hi,

 

I have strings submitted by the users of my website in the following format

 

$string = "text goes here

[codetag]some code goes here[/codetag]

more text

[codetag]more code here[/codetag]

... (and so on) ..."

 

and I want to handle the parts of the string inside the [codetag] tags different than the parts outside the tags. What's the best way to do this?

 

I guess one way is to split it into two arrays that on the example above would look like something this:

$texts = ["text goes here\n","\nmore text\n","\n ... (and so on) ..."]

$codes = ["some code goes here","more code here"]

 

How do I do that?

Link to comment
Share on other sites

The best way would be to use regex.

 

ex:

 

$text = preg_replace("(\[codetag\](.+?)\[\/codetag])is",'<code>$1</code>', $text);

 

preg_replace() The example will replace all text inside of [codetag][/codetag] with <code>text here</code>

Thanks, but I need to perform at least one more function on each part of the string that are outside the codetags; for example \n\n should be replaced by </p><p>, but not inside the <code> tags.

 

Ie. if $string = "First paragraph.\n\nSecond paragraph.[codetag]Code\n\nA few lines later[/codetag]." then the output should be "First paragraph</p><p>Second paragraph</p><code>Code\n\nA few lines later</code>."

 

... so I guess I need another way to do it.

Link to comment
Share on other sites

You can try something like:

 

$string = "First paragraph.\n\nSecond paragraph.[codetag]Code\n\nA few lines later[/codetag].";
$split = preg_split('~\[/?codetag\]~', $string);
for($i = 0;$i < count($split);$i++)
$out .= ($i % 2) ? '</p><code>' . $split[$i] . '</code>' : str_replace("\n\n", '</p><p>', $split[$i]);

echo $out;

Link to comment
Share on other sites

Different ways to skin a cat...I suppose an option could include:

 

$string = "First paragraph.\n\nSecond paragraph.[codetag]Code\n\nA few lines later[/codetag].";

function replacement($match){
    $match[0] = str_replace('</p><p>', "\n\n", $match[0]);
    $match[0] = preg_replace('#(?:\[codetag\])(.+?)(?:\[/codetag\])#is', '<code>$1</code>', $match[0]);
    return $match[0];
}

$string = str_replace("\n\n", '</p><p>', $string);
$string = preg_replace_callback('#\[codetag\].+?\[/codetag\]#is', 'replacement', $string);
echo $string;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.