Forum Moderators: coopster

Message Too Old, No Replies

Using a - with strstr gives me errors

Is this not allowed?

         

jake66

3:20 am on Feb 21, 2008 (gmt 0)

10+ Year Member



$cut = strstr($listing['name'], '-', true);

Gives me:
Warning: Wrong parameter count for strstr() in...

When I remove true, it works. But the - is still seen, and I'd like to cut it off of this result, without impacting the actual database entry.

eelixduppy

3:57 am on Feb 21, 2008 (gmt 0)



The third parameter, if you read the documentation for that function, isn't available until php 6.0.0. You are going to have to work out another way to get the first part of that string without strstr. strpos and substr are probably your next best choices as far as that goes.

jake66

5:30 am on Feb 21, 2008 (gmt 0)

10+ Year Member



Bummer!
I did not realize those update logs meant php version, never paid much attention to them.

I looked at strpos(), as php.net said it was not so much of a memory hog.. but I cannot figure out how to put this into code using strpos().

strstr() seemed pretty basic though.

eelixduppy

6:01 am on Feb 21, 2008 (gmt 0)



Something like this, maybe:

$cut = substr($listing['name'], 0, strpos($listing['name'], '-'));

jake66

6:47 am on Feb 21, 2008 (gmt 0)

10+ Year Member



Thanks!

Is there anyway to strip everything before the dash? Right now, everything after it is removed.

I tried:
Replacing the 0,
with:
-0, 0
..but got errors.

eelixduppy

6:49 am on Feb 21, 2008 (gmt 0)



Change it to this:

$cut = substr($listing['name'], strpos($listing['name'], '-'));

I removed the "length" parameter and made the start position where it finds the hyphen. Just curious, though, because you can actually use strstr to do this same thing. I thought you wanted to string before the hyphen, and that's why I suggested the use of substr.

jake66

9:03 am on Feb 21, 2008 (gmt 0)

10+ Year Member



That one still shows the dash too. Is there any way to hide it?

The results are currently like this:
Result Name - Result Description

With the latest code, it looks like:
- Result Description

Anyway to clip the - ? I tried adding/removing the spaces, still no luck.

eelixduppy

3:45 pm on Feb 21, 2008 (gmt 0)




$cut = trim(substr($listing['name'], strpos($listing['name'], '-')+1));

:)

PHP_Chimp

7:42 pm on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or if you dont want to use 3 functions you could use -

$cut = preg_replace('%^[^-]+-(.+)%', '$1', $listing['name']);

;)

eelixduppy

7:51 pm on Feb 21, 2008 (gmt 0)



Or if you want to get technical, the following runs consistently faster than both our previous examples ;)

$cut = trim(strstr($string, '-'),'-');

PHP_Chimp

8:00 pm on Feb 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lol you win :)

jake66

4:52 am on Feb 29, 2008 (gmt 0)

10+ Year Member



Awesome, thanks very much for the help guys!