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

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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.

Answer*

Draft saved
Draft discarded
Cancel
0

default

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