Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

2 of 2
In English, the pronoun 'I' is capitalized; 'JavaScript' is the proper capitalization; 'Thanks' is unnecessary noise; PHP stands for PHP: Hypertext Preprocessor.
Rikesh
  • 26.5k
  • 14
  • 82
  • 90

Convert code from JavaScript to PHP

My code exist some things like "< i585 >". I wan to get string from "i" to ">". And replace "585" instead of "< i585 >". I write some code in JavaScript.

<script type="text/javascript">
var foo ="M <i585> <i646>";
while (foo.indexOf("<i") != -1) {
 var indexBaş = foo.indexOf("<i");
 var indexSon = foo.indexOf(">", indexBaş);
 var id = foo.substring(indexBaş + 2, indexSon);
 foo = foo.substring(0 , indexBaş) + id + foo.substring(indexSon + 1 , foo.lenght);
 
}
document.write(foo);
</script>

But I have to convert this code to php. So I write this code

$foo ="M <i585> <i646>";
$start = 0;
$kacTane= 0;
for($i = 0 ; $i < strlen($foo) ; $i++ ) {
 if($foo[$i] == "<") {
 if(($foo + 1 )< strlen($foo)) {
 if($foo[$i+1] == "i") {
 $kacTane++;
 }
 }
 
 }
}
for($i = 0; $i < $kacTane; $i++) {
 $ilkIndex = strpos($foo , "<" , $start);
 $sonindex = strpos($foo , ">" , $ilkIndex);
 
 $id = substr($foo , $ilkIndex + 2 ,( $sonIndex -3) - $ilkIndex );
 
 $first = substr($foo , 0 , $ilkIndex +2);
 $second = substr($foo , $sonIndex + 1 , strlen($foo) - $sonIndex - 1 );
 
 $foo = "$first$id$second";
 $start = ($sonindex + 1);
}
echo $foo;

But it isn't work.

Sorry for bad English.

oznakn
  • 90
  • 3
  • 9
default

AltStyle によって変換されたページ (->オリジナル) /