开发者

Java String.valueOf to PHP

Hope all is well. So I am new to PHP and I have to write a program in it that basically parses through information. I have been doing oodles of research and finally found something that work开发者_StackOverflow中文版s for what I need but it is written in Java. I ran into a wall when I came to this (the 3rd line, tempChar)

for (counter = 0; counter <= htmlInput.length()-1; counter++){
  //place the current character in tempChar
  tempChar = String.valueOf(htmlInput.charAt(counter));

Is the equivalent of string.valueOf implode in php? I just need to return the string representation and store it's contents. Thank you so much for helping me while I am a noob at this.


The java code you posted looks like it's converting a char to a java.lang.String. This is necessary in java since it's a strongly typed language. PHP is loosely typed so you don't need to explicitly handle type conversions. The PHP conversion of the java code you found might look something like this:

for ($counter=0; $counter <= count($htmlInput) - 1; $counter++) {
    // place the current character in tempChar
    $tempChar = substr($htmlInput, $counter, 1);

BTW: implode() in PHP is used to join elements of an array into a string.


foreach (str_split($string) as $char) {
    ...
}

asking for equivalents of specific functions won't get you anywhere; you need to take a wider view to translate code effectively. In fact, even this is probably wrong, because there's a good chance that you don't need to loop over the characters in the string at all — but it's impossible to tell since you didn't provide any information about what you're really doing.


You can get a specific character from a string in PHP treating the string like an array:

$tempChar = $htmlInput[$counter];

This would be basically the same thing.

(Another form I just learned about: $htmlInput{$counter} - notice the curly braces.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜