blufish Posted June 3, 2008 Share Posted June 3, 2008 Hey guys, I need to know how to find what character is at a certain position, like If I told php I wanted to know what the 4th character in a string which was "hello world!" It would output "l" anybody know how to do this or something like this? any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/108536-solved-find-what-character-is-at-what-position/ Share on other sites More sharing options...
trq Posted June 3, 2008 Share Posted June 3, 2008 <?php $s = "hello world"; echo $s[3]; ?> You might also take a look at substr. Quote Link to comment https://forums.phpfreaks.com/topic/108536-solved-find-what-character-is-at-what-position/#findComment-556549 Share on other sites More sharing options...
blufish Posted June 3, 2008 Author Share Posted June 3, 2008 it doesn't seem to be working, here is the code I have been making: <?php $code = $_POST['code']; $leng = strlen($code); $ncode = "<script type='text/javascript'>"; while($leng>0) { $ncode = $ncode."var a".$leng." = ".substr($ncode, $leng).";"; $leng = $leng-1; } $ncode = $ncode."</script>"; echo $ncode; ?> ...Trying to make my own script confuzzler ... To explain what it's supposed to do, It's supposed to make a javascript which defines a bunch of variables based on the position of characters in the string. I also tried this, It doesn't work: <?php $code = $_POST['code']; $leng = strlen($code); $ncode = "<script type='text/javascript'>"; while($leng>0) { $ncode = $ncode."var a".$leng." = ".$code[$leng].";"; $leng = $leng-1; } $ncode = $ncode."</script>"; echo $ncode; ?> Quote Link to comment https://forums.phpfreaks.com/topic/108536-solved-find-what-character-is-at-what-position/#findComment-556556 Share on other sites More sharing options...
jonsjava Posted June 3, 2008 Share Posted June 3, 2008 When I run your code: <?php $code = "this is a test"; $leng = strlen($code); $ncode = "<script type='text/javascript'>\n"; while($leng>0) { $ncode = $ncode."var a".$leng." = ".$code[$leng].";\n"; $leng = $leng-1; } $ncode = $ncode."</script>"; echo $ncode; ?> I get <script type='text/javascript'> var a14 = ; var a13 = t; var a12 = s; var a11 = e; var a10 = t; var a9 = ; var a8 = a; var a7 = ; var a6 = s; var a5 = i; var a4 = ; var a3 = s; var a2 = i; var a1 = h; </script> What am I supposed to be seeing? Quote Link to comment https://forums.phpfreaks.com/topic/108536-solved-find-what-character-is-at-what-position/#findComment-556587 Share on other sites More sharing options...
blufish Posted June 3, 2008 Author Share Posted June 3, 2008 That seems right, for what I've done in the script so far. Did you change the code or something? I'll try the code again... Quote Link to comment https://forums.phpfreaks.com/topic/108536-solved-find-what-character-is-at-what-position/#findComment-557002 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.