user2594154
user2594154

Reputation: 489

Get values in between specific tag using PHP GetBetween

I would like to split text in between < li> and < /li> tag contents and display into a textarea.

My database values are saved in following format

<ul>
  <li>Test1</li>
  <li>Test2</li>
  <li>Test3</li>
  <li>Test3</li>
</ul>

I would like to fetch each value in between < li > and < /li > and display in a textarea. (maximum only 4 text-areas).

<?php
$sql="SELECT * FROM product_name";
$result=mysql_query($sql) or die(mysql_error());        
$row = mysql_fetch_array($result);

function GetBetween($value1="",$value2="",$dbvalue){
$temp1 = strpos($pool,$value1)+strlen($value1);
$result = substr($dbvalue,$temp1,strlen($dbvalue));
$dd=strpos($result,$value2);
if($dd == 0){
$dd = strlen($result);
}

return substr($result,0,$dd);
?>  


<textarea  name="description1">
<?php echo GetBetween("<li>","</li>",$row['description']);?>
</textarea>

<textarea  name="description2"></textarea>
<textarea  name="description3"></textarea>
<textarea  name="description4"></textarea>

In my first Textarea database values are fetching. But I do not know how to display 2,3 and 4 each values into the respective Textares.

I am expecting following output:

<textarea  name="description1">Test1</textarea>
<textarea  name="description2">Test2</textarea>
<textarea  name="description3">Test3</textarea>
<textarea  name="description4">Test4</textarea>

Please help me. Thanks

Upvotes: 0

Views: 114

Answers (2)

CASH
CASH

Reputation: 93

I suggest an example:

<?php
$str = "<ul>
  <li>Test1</li>
  <li>Test2</li>
  <li>Test3</li>
  <li>Test3</li>
</ul>";

preg_match_all('/<li ?.*>(.*)<\/li>/',$str,$matches);

if(!empty($matches[1])){
   foreach($matches[1] as $key=>$text){
      echo '<textarea  name="description'.($key+1).'">'.$text.'</textarea>';
   }
}
}

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

Here's a simple example that should get you started:

<?php

$html = <<<HTML
<ul>
  <li>Test1</li>
  <li>Test2</li>
  <li>Test3</li>
  <li>Test3</li>
</ul>
HTML;

$doc = new DOMDocument;
$doc->loadHTML($html);

$id = 1;
// find all <li> elements
foreach ($doc->getElementsByTagName('li') as $element) {
    // then print <textarea> elements
    printf('<textarea name="description%d">%s</textarea>',
        $id++,
        htmlspecialchars($element->nodeValue)
    );
}

Upvotes: 1

Related Questions