Jump to content

php get part of a string


desithugg

Recommended Posts

Umm, I was wondering if I could use php and get certain parts of a string like

$string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; 

Would it be possiable to use something like preg match to get only that part Desithugg into another string like $name.

So I want to get the part between <td>Name</td><td> and </td></tr>.

If it's possiable can anyone give me an example.

I tried looking at the manual but didn't quite get it.

Link to comment
https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/
Share on other sites

<?php

function textbetweenarray($s1,$s2,$s){
  $myarray=array();
  $s1=strtolower($s1);
  $s2=strtolower($s2);
  $L1=strlen($s1);
  $L2=strlen($s2);
  $scheck=strtolower($s);

  do{
  $pos1 = strpos($scheck,$s1);
  if($pos1!==false){
$pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
if($pos2!==false){
  $myarray[]=substr($s,$pos1+$L1,$pos2);
  $s=substr($s,$pos1+$L1+$pos2+$L2);
  $scheck=strtolower($s);
  }
	}
  } while (($pos1!==false)and($pos2!==false));
return $myarray;
}

$string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; 
$tds = textbetweenarray("<td>", "</td>", $string);

echo $tds[1];

?>

 

$tds -> array with each occurence of text between each <td>

 

:)

Nice little function Chig.

 

To end the question on how to assign $name that value try this:

 

<?php
function textbetweenarray($s1,$s2,$s){
  $myarray=array();
  $s1=strtolower($s1);
  $s2=strtolower($s2);
  $L1=strlen($s1);
  $L2=strlen($s2);
  $scheck=strtolower($s);

  do{
  $pos1 = strpos($scheck,$s1);
  if($pos1!==false){
$pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
if($pos2!==false){
  $myarray[]=substr($s,$pos1+$L1,$pos2);
  $s=substr($s,$pos1+$L1+$pos2+$L2);
  $scheck=strtolower($s);
  }
	}
  } while (($pos1!==false)and($pos2!==false));
return $myarray;
}

$string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; 
$tds = textbetweenarray("<td>", "</td>", $string);

$$tds[0] = $tds[1]; // assigns the value of $tds[0] (name the value of $tds[1])

echo $Name;
?>

 

=)

To add: the best thing to do when parsing table rows is this:

 

<?php

function textbetweenarray($s1,$s2,$s){
  $myarray=array();
  $s1=strtolower($s1);
  $s2=strtolower($s2);
  $L1=strlen($s1);
  $L2=strlen($s2);
  $scheck=strtolower($s);

  do{
  $pos1 = strpos($scheck,$s1);
  if($pos1!==false){
$pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
if($pos2!==false){
  $myarray[]=substr($s,$pos1+$L1,$pos2);
  $s=substr($s,$pos1+$L1+$pos2+$L2);
  $scheck=strtolower($s);
  }
	}
  } while (($pos1!==false)and($pos2!==false));
return $myarray;
}

$string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; 
$trs = textbetweenarray("<tr>", "</tr>", $string);

foreach($trs as $tr) {
$tds = textbetweenarray("<td>", "</td>", $tr);
foreach($tds as $td) {
  // Do something
}
}

?>

Umm a lil problem

I only gave, the function works fine but the thing is I don't want stuff between evry <td> and </td>

Maybe I didn't give a good example, I want to extract a number of strings from the big string, but i only put one in the example.

<?
$string = "<table><tr><td>Name</td><td>Desithugg</td></tr>
<tr><td>Age</td><td>14</td></tr>
<tr><td>Gender</td><td>Male</td></tr></table>";
?>

So lets say I want to extract only the answers.

so I want

what's between <tr><td>Name</td><td> and </td></tr><tr><td>Age</td> in the string $answers['0'];

than i want

what's between <tr><td>Age</td><td> and </td></tr><tr><td>Gender</td> in the string $answers['1'];

and

what's between <tr><td>Gender</td><td> and </td></tr></table> in the string $answers['2'];

So basically I want to run the function number of times and each time looking for something different.

I tried to run the function you gave me twice but didn't work, I think it just looks for stuff between every <td> and </td>

But I don't want to get stuff between every one of them, Because there will be some info that I don't need.

<?php

function textbetweenarray($s1,$s2,$s){
  $myarray=array();
  $s1=strtolower($s1);
  $s2=strtolower($s2);
  $L1=strlen($s1);
  $L2=strlen($s2);
  $scheck=strtolower($s);

  do{
  $pos1 = strpos($scheck,$s1);
  if($pos1!==false){
$pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
if($pos2!==false){
  $myarray[]=substr($s,$pos1+$L1,$pos2);
  $s=substr($s,$pos1+$L1+$pos2+$L2);
  $scheck=strtolower($s);
  }
	}
  } while (($pos1!==false)and($pos2!==false));
return $myarray;
}

$string = "<table><tr><td>Name</td><td>Desithugg</td></tr>
<tr><td>Age</td><td>14</td></tr>
<tr><td>Gender</td><td>Male</td></tr></table>";

$output = array();

$trs = textbetweenarray("<tr>", "</tr>", $string);

foreach($trs as $tr) {
echo $tr;
$tds = textbetweenarray("<td>", "</td>", $tr);
$first = $tds[0];
$second = $tds[1];
$output[$first] = $second;
}

echo "<pre>";

foreach($output as $k => $v) {
echo "$k = $v\n";
}

echo "/<pre>";

?>

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.