Jump to content

[SOLVED] Get string between 2 delimiters


hlstriker

Recommended Posts

I'm trying to get a string between 2 different delimiters then save it to an array.

 

Example (delimiters should be <tr> and </tr>):

<tr><td>Text 1</td></tr>

<tr><td>Text 2</td></tr>

<tr><td>Text 3</td></tr>

 

This should add the 3 to an array:

$array[0] = <td>Text 1</td>

$array[1] = <td>Text 2</td>

$array[2] = <td>Text 3</td>

 

I thought maybe explode() would work but I couldn't get it.

 

Maybe someone can inform me :)

Link to comment
https://forums.phpfreaks.com/topic/122272-solved-get-string-between-2-delimiters/
Share on other sites

$string = '<tr><td>Text 1</td></tr>
<tr><td>Text 2</td></tr>
<tr><td>Text 3</td></tr>';
$myMatches = array();
if(preg_match_all("~\<tr\>(.+?)\<\/tr\>~",$string,$matches)){
foreach($matches[1] as $match){
	$myMatches[] = htmlentities($match);
}

}

print_r($myMatches);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.