How to choose between explosion and regex

My line contains some form hotkeys [hotkey]

. For example:

"This is a sample string [red] [h1]"

      

When I process this line with a php function, I would like the function to output the original line like this;

<font color='red'><h1>This is a sample string</h1></font>

      

I would like to use this function purely for convenience, facilitating some typing. I can use a font tag or div or whatever, without getting into it. This is the point; hotkey will cause the original string to be wrapped in

<something here>original string<and something there>

      

So the function must first determine if there are any hotkeys or not. It's simple; just check if existence exists[

Next, we need to process the line to determine what hotkeys exist and enter the biz logic as to which shells will be deployed.

and finally, we have to clear the original line of hotkeys and put the results back.

My question is if there is a regex that will make this more efficient than the next parsing method I plan to implement as a function.

step 1 explode string into array using delimiter [

step 2 go through each element of the array to see if the closing is present ]

and it forms one of the defined hotkeys, and if so, do the necessary.

Obviously this method does not use any power of the regexp. I'm wondering if regex can help here. Or, can you suggest a better way to do this?

+3


source to share


2 answers


If [

and ]

are the only delimeters you need to worry about, you can probably usestrtok



0


source


I don't speak English well, but I saw your example:

"This is a sample string [red] [h1]"

<font color='red'><h1>This is a sample string</h1></font>

      



If I were you:

$red = substr( $chaine, strpos($chaine, '['), strpos($chaine, ']') );

      

0


source







All Articles