Help!php Posted February 8, 2012 Share Posted February 8, 2012 I am trying to get a paragraph from a website. Everything works but the first letter is kept on missing. The code shown below is used to get the paragraph. Not sure why the first letter is missing. Please help!!! if ( isset( $offer[ 0 ] ) ) { // Tidy it up - remove commas and weird Word chars $off = strip_tags( $offer[ 0 ] ); $off = substr( $off, strpos( $off, ";" ) + 1 ); $off = str_replace("'", "", $off ); $off = trim( $off ); } else { $off = "0"; } Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/ Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 Is the letter there if you echo the value of $offer[0] before it's passed through all of those string functions? Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315824 Share on other sites More sharing options...
ManiacDan Posted February 8, 2012 Share Posted February 8, 2012 $off = substr( $off, strpos( $off, ";" ) + 1 ); If there is no ';' in $off, that line will trim the first letter off $off. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315825 Share on other sites More sharing options...
Help!php Posted February 8, 2012 Author Share Posted February 8, 2012 Yh it does print the result. But some of the result is printer like I want to get something from a website + I want to get something from a websiteI want to get something from a website First line is splitted with + the other one is not. I ran the code without $off = substr( $off, strpos( $off, ";" ) + 1 ); No luck Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315842 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 How about a sample of actual data, the output you expect from the code, and what you actually get from it? Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315858 Share on other sites More sharing options...
Help!php Posted February 8, 2012 Author Share Posted February 8, 2012 Actual data : Free 3 Year On-Site Warranty Output I am expecting is : Free 3 Year On-Site Warranty What I am getting now is : ree 3 Year On-Site Warranty Cheers Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315860 Share on other sites More sharing options...
ManiacDan Posted February 8, 2012 Share Posted February 8, 2012 That code is definitely the problem, I just ran your code with and without it: php > $offer = array('Free 3 Year On-Site Warranty'); php > php > if ( isset( $offer[ 0 ] ) ) php > { php { $off = strip_tags( $offer[ 0 ] ); php { $off = substr( $off, strpos( $off, ";" ) + 1 ); php { $off = str_replace("'", "", $off ); php { $off = trim( $off ); php { } else { php { $off = "0"; php { } php > php > echo $off; ree 3 Year On-Site Warranty php > php > php > php > $offer = array('Free 3 Year On-Site Warranty'); php > php > if ( isset( $offer[ 0 ] ) ) php > { php { $off = strip_tags( $offer[ 0 ] ); php { //$off = substr( $off, strpos( $off, ";" ) + 1 ); php { $off = str_replace("'", "", $off ); php { $off = trim( $off ); php { } else { php { $off = "0"; php { } php > php > echo $off; Free 3 Year On-Site Warranty See? Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315865 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 EDIT: Composing while Dan was posting, apparently . . . It seems to be exactly what ManiacDan said it was. If there is no semicolon, the first letter is stripped, so check for the presence of a semicolon before allowing the substr() function. // Tidy it up - remove commas and weird Word chars $off = strip_tags( $offer[0] ); //$off = substr( $off, strpos( $off, ";" ) + 1 ); COMMENTED OUT THIS LINE $off = strpos($off, ';') !== FALSE ? substr( $off, strpos( $off, ";" ) + 1 ) : $off; // ADDED THIS LINE $off = str_replace("'", "", $off ); $off = trim( $off ); Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315867 Share on other sites More sharing options...
ManiacDan Posted February 8, 2012 Share Posted February 8, 2012 Pika, you may be interested to learn that the && can be used as a quick shorthand IF: strpos($off, ';') !== FALSE && $off = substr( $off, strpos( $off, ";" ) + 1 ); It's sloppy and perl-like and will cause any code reviewers to punch you, but it's valid. I think it's fun. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315878 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 I've never seen it used that way. It would be my luck that I'd use it and the forget what it does. Then I'd be punching myself later. Again. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315881 Share on other sites More sharing options...
ManiacDan Posted February 8, 2012 Share Posted February 8, 2012 You totally shouldn't do it...but you could. You should have seen my face when I was trying to debug a perl module and realized that the window was too narrow to show me the "unless" clauses off on the right side of the lines. I was so confused. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315883 Share on other sites More sharing options...
Pikachu2000 Posted February 8, 2012 Share Posted February 8, 2012 I can picture that happening. Hell, it's happened to me here, with the horizontal scrolling in the divs that the php and code tags sometimes cause. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315885 Share on other sites More sharing options...
sasa Posted February 8, 2012 Share Posted February 8, 2012 Pika, you may be interested to learn that the && can be used as a quick shorthand IF: strpos($off, ';') !== FALSE && $off = substr( $off, strpos( $off, ";" ) + 1 ); It's sloppy and perl-like and will cause any code reviewers to punch you, but it's valid. I think it's fun. you can use or toostrpos($off, ';') == FALSE || $off = substr( $off, strpos( $off, ";" ) + 1 ); Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315893 Share on other sites More sharing options...
ManiacDan Posted February 8, 2012 Share Posted February 8, 2012 We have to stop giving this poor girl such horrible ideas. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1315904 Share on other sites More sharing options...
Help!php Posted February 9, 2012 Author Share Posted February 9, 2012 Thank you very much for your help! That would a girl. Cheers. Link to comment https://forums.phpfreaks.com/topic/256674-first-letter-is-missing-something-wrong-with-my-php/#findComment-1316071 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.