Jump to content

Recommended Posts

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";
		}

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

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?

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

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

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 too
strpos($off, ';') == FALSE || $off = substr( $off, strpos( $off, ";" ) + 1 );

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.