Jump to content

file string manipulation...


mkosmosports

Recommended Posts

Hey,

 

Ive got a file which contains around 150 lines of the following html:

 

<option value="56">Team Canada</option>

 

What I need is to transform each of these lines to be:

 

<div class="teamlist"><a href="team.php?team=56" target="_blank">Team Canada</a></div>

 

Im thinking the easiest way to accomplish this is to get "Team Canada" and "56" into an array, where "56" would be the associative key to "Team Canada".

 

Any ideas how I could accomplish this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/52230-file-string-manipulation/
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;
}

$source = file_get_contents("html.html");

$options = textbetweenarray("<option ", "/option>", $source);

foreach($options as $option) {
$id = textbetweenarray("value=\"", "\"", $option);
$id = $id[0];
$teamname = textbetweenarray(">", "<", $option);
$teamname = $teamname[0];
echo "<div class=\"teamlist\"><a href=\"team.php?team=$id\" target=\"blank\">$teamname</a></div>\n";
}

?>

 

Not tested, should work though :)

try

 

<?php
$data = 'blare <option value="56">Team Canada</option> ghfdsgfh gfj dsfj
blare <option value="52">Team USD</option> ghfdsgfh gfj dsfj
blare <option value="55">Team England</option> ghfdsgfh gfj dsfj
blare <option value="56">Team france</option> ghfdsgfh gfj dsfj
';
echo preg_replace('%<option value="(\d+)">(.*)</option>%i', '<div class="teamlist"><a href="team.php?team=$1" target="_blank">$2</a></div>', $data );
?>

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.