Jump to content

$index=strpos(substr($data[0]['long_descr'],0,20),'same_as');


M477HEW

Recommended Posts

I'm really new to PHP and am currently using the framework of an existing site to help me learn the ropes.

 

I have the following line of code and I'm kind of stuck.  I'm OK up to the ['long_descr'] but after that I am lost.  Can anyone help me out and explain this line of code to me please?  I'm particularly confused by the last half - ,0,20 etc. What do these numbers represent?

 

$index=strpos(substr($data[0]['long_descr'],0,20),'same_as');

 

Thanks in advance for any help

 

Matt

Link to comment
Share on other sites

For beginners, even the documentation can be confusing. First off, let's break it down. The line of code you posted uses two functions. strpos() and substr().

 

The strpos() function is used to find the first occurrence of a string within another string. Take the below as an example. The resulting value of the variable $pos would be 0 because a is the very first character in the string (it starts at 0, not 1).

 

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

 

The substr() function returns a specific section of a string which you tell to find by providing where in that string to start and where to end. Take the below code as an example. The resulting value of $return would be Hello because you are telling it to return only the part of the string starting from 0, (the beginning), and ending at 4, (the letter "o" is in the fourth position where the letter "H" would be position zero).

 

$mystring = 'Hello my name is Matt!';
$return = substr($mystring, 0, 4);

 

With that in mind, the line of code you posted grabs the first 21 characters of a string which is, from the looks of it, a description of something ($data[0]['long_descr']). (Note, spaces count as a character). It then searches for the first occurrence of "same_as" in that string and stores the position of that first occurance in the variable $index. The following bit of code make make it a little easier to read.

 

$haystack = substr($data[0]['long_descr'],0,20);
$index = strpos($haystack,'same_as');

 

Correct me if I am wrong, but I am fairly certain that is what that line of code does.

Link to comment
Share on other sites

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.