1

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 works 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.

Frank
  • 35
  • 4
  • valueOf will take that char and convert it into a String type. Are you sure you wanna do that? – Sagar V Aug 04 '11 at 04:10
  • Also have a look at this http://stackoverflow.com/questions/28098/php-tostring-equivalent – Sagar V Aug 04 '11 at 04:12
  • What does the Java code actually do? – NullUserException Aug 04 '11 at 04:21
  • It will go through a html table and parse all the data, throwing into a .csv file. Here is a link to it: http://www.cs.iupui.edu/~aharris/H2Text.html – Frank Aug 04 '11 at 04:40
  • The page won't open here, but there are easier ways to do this - ie: parsing the HTML using an (X)HTML parser (see [this question](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php)) and go from there – NullUserException Aug 04 '11 at 04:53

3 Answers3

1

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.

Asaph
  • 159,146
  • 25
  • 197
  • 199
0
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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • Hey Hobbs! Basically I am reading in a file, parsing through it, finding key terms, and throwing them out into a .csv file with php. The best example I have found, is here: http://www.cs.iupui.edu/~aharris/H2Text.java – Frank Aug 04 '11 at 04:45
0

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.)

OverZealous
  • 39,252
  • 15
  • 98
  • 100