TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-10-2008, 11:52 AM   #1 (permalink)
The Contributor
 
pipesportugal's Avatar
 
Join Date: May 2008
Location: Oporto-Portugal
Posts: 32
Thanks: 11
pipesportugal is on a distinguished road
Default Help with function stristr()

Hello everyone,

I am using the above mentioned function in the following way:

PHP Code:
$text0 "int(7)";

$text1 stristr($text0'(');
echo 
$text1// ok! outputs (7)

$text2 stristr($text0'('true);
echo 
$text2// I wanted to retrieve the word -> int 
I am getting the following error on the second instruction:
Warning: Wrong parameter count for stristr()

I've checked the manual and everything seems to be ok, so what am I doing wrong ?

Can someone help?
pipesportugal
pipesportugal is offline  
Reply With Quote
Old 06-10-2008, 12:26 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The third argument (before_needle) is only available with PHP 5.3.0 and upward.

An equivalent snippet would be something like:
PHP Code:
$text2 = (FALSE !== ($pos strpos($text0'('))
       ? 
substr($text00$pos)
       : 
FALSE
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
pipesportugal (06-11-2008)
Old 06-10-2008, 02:08 PM   #3 (permalink)
The Contributor
 
pipesportugal's Avatar
 
Join Date: May 2008
Location: Oporto-Portugal
Posts: 32
Thanks: 11
pipesportugal is on a distinguished road
Default

Hi Salathe,

I really was not expecting anyone to reply so soon, as my idea is(was...) that most talkphp members were on the left side of the Atlantic Ocean therefore by time I've sent this thread through, I thought everyone was still spleeping.

As You can (easily) imagine my routine was reading the result of a mySQL query "SHOW COLUMNS FROM table" and then I was using this routine to desintegrate column 2: example: int(7) into: $type=int and $length=7, as I want to develop a simple form generator class(by my own).

In the meanwhile I changed approach and went by this road:
$this->query = "SELECT COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_MAXIMUM_LENGTH, COLUMN_COMMENT from information_schema.columns where table_name = '$table'";

This sql instruction gives me directly the fields that I was looking for.

Regarding Your precious advice, those lines execute its purpose but I must confess that it's difficult for me to "read" Your php lines, as I have never done anything like that before. Looks complicated...from rookie my point of view. Some instructions how to read them in plain English would be nice...

Thanks for caring,
Cheers,
Jose.
pipesportugal is offline  
Reply With Quote
Old 06-10-2008, 03:01 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Salathe doesn't sleep. He's out there, maintaining the balance.

It might be harder to read if you're not first familiar with the ternary operator. It's a really handy shorthand for a basic IF statement.

PHP Code:
/* in english */
if (statement evaluates true) ? 'then do this' 'otherwise do this';

/* in PHP */

// echo's "$somevar is set" if true
echo (isset($somevar)) ? '$somevar is set' '$somevar is not set';

// sets $isSet to true, if true
$isSet = (isset($somevar)) ? true false;

// sets $answer to "$somevar is less than or equal to $othervar" if true
$answer = ($somevar <= $othervar) ? '$somevar is less than or equal to $othervar' '$othervar is less than $somevar'
So what Salathe has done;
If $pos = strpos($text0, '(')) doesn't return false, than that means '(' was found somewhere in $text0 and the statement evaluates to true. If it evaluates to true, the ternary executes the code found after the ? and before the :, which in this case returns a substr of $text0, from the start of the string to the position where the first ( was found. If it evaluates to false, it executes the code found after the : and before the end of the block, which in this case would return a value of FALSE.
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
pipesportugal (06-11-2008)
Old 06-10-2008, 04:26 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Salathe has made use of the ternary operator, it works like this:

( expression) ? ( expression2 ) : (expression3)

If expression is true, then PHP will evaluate to ( expression2 )
If expression1 is false, then it evaluates to ( expression3 )

To help you understand it further it can be rewritten like so:

PHP Code:
if(($pos strpos($text0'(')) !== FALSE ){
    
$text2 substr($text00$pos);
}else{
    
$text2 FALSE;

So, first PHP creates '$pos' and assigns the result of 'strpos' to it, then it functions like a regular IF, so if $pos is not equal to FALSE, or $pos not of the same type (which is what the !== comparison operator means, this is used because it will return an int on success and a bool false if it failed) then assign result of substr to $text2, else assign it FALSE.

As you can see using the ternary is smaller but at the same time can be tricky to read, especially if you nest them, which you shouldnt do, not unless you want to give yourself a headache, look at this snippet posted by salathe in another post:
PHP Code:
echo chr(16 + ((strlen('talkphp') + 1337) >> ((true === false
     ? ((
'a' === chr(0x61)) ? 'talkphp' 'php'
     : ((
767 & (405 - (pow(12* ('0' 
     
false true))))))))), 
     
chr('happy' 0x29 0x28); 
edit : damn delayedinsanity beat me to it, i must learn to type faster!
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following 2 Users Say Thank You to sketchMedia For This Useful Post:
Matt (06-10-2008), pipesportugal (06-11-2008)
Old 06-10-2008, 05:34 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Yeah, but you explained it a lot better. -m
delayedinsanity is offline  
Reply With Quote
Old 06-10-2008, 05:40 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Bonus points to sketch for finding my example of "when ternary goes bad!".
Salathe is offline  
Reply With Quote
Old 06-11-2008, 08:14 PM   #8 (permalink)
The Contributor
 
pipesportugal's Avatar
 
Join Date: May 2008
Location: Oporto-Portugal
Posts: 32
Thanks: 11
pipesportugal is on a distinguished road
Default

Dear Friends,

Understood the basic about the ternary operator and some tests made with sucess.

Are there any limit to the "thank You" I can give to my forum colleagues?

I am new but I suspect I am gone give quite a few in the next times.

Thank You all,
pipesportugal
pipesportugal is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:26 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design