Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-24-2009, 08:48 PM str_replace & split
andypugh's Avatar
Novice Talker

Posts: 14
Trades: 0
Hi there, I was hoping someone could help me with a little bit of code I'm currently writing, to explain what I'm trying to do -
The script I have written is a modification to Sam Broadcaster to allow for text requests via http on Oseki Server Manager.

Basically, a user will submit a text message - say - "Bon Jovi - Livin On A Prayer"
I was then using split to take it apart at the dash and query the database under artist/title. However, I'm attempting to write a suitable method that will allow me to allow for all instances , such as "Bon Jovi-Livin on a prayer" & "Bon Jovi- Livin on a prayer" You see the spacing changes? as I'm using WHERE ARTIST LIKE '%$artist%', effectively the whitespace is breaking it..

Really hope I'm making sense so far, I have a tendency to rant a little ! Have a look at my code...

PHP Code:

$data 
$_GET["songname"]; // GET SONG NAME FROM GET STRING I.E LOCALHOST/REQ.PHP?SONGNAME=...
echo "$data<br/><br/>"// ECHO RAW DATA FOR TEST

 
$alldata str_replace(" - ""-"$data); // REPLACE - WITH 2 SPACES
 
$alldata str_replace("- ""-"$data); // REPLACE - WITH 1 SPACE

echo "$alldata<br/><br/>"// ECHO REPLACED DATA


list($artist$title) = split('[-]'$alldata); // SPLIT REPLACED DATA
echo "artist: \"$artist\"<br/>"// ECHO ARTIST
echo "title: \"$title\"<br/>"// ECHO TITLE



$result mysql_query("select * from songlist WHERE artist LIKE '%$artist%' AND title LIKE '%$title%' order by ID DESC LIMIT 0,1");

while(
$r=mysql_fetch_array($result))

{    
  
  
$ID=$r["ID"]; // GET THE ID WHICH THEN SENDS TO SAM BROADCASTER
   
 
  

Where I am effectively stuck is on the str_replace, I'm aware that by replacing an instance of a dash without a space, it's overwriting any other method... Is there something else I can do? Or certainly a mysql query that would allow for the whitespace on the beginning of the 'search' ?

Hope this makes sense, would really appreciate any help!
Cheers,
Andy
__________________

Please login or register to view this content. Registration is FREE
- THE Harry Potter Resource!

Last edited by andypugh; 03-24-2009 at 08:50 PM..
andypugh is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-24-2009, 09:18 PM Re: str_replace & split
andypugh's Avatar
Novice Talker

Posts: 14
Trades: 0
Hi Guys, I think I've managed to fix it, I set up an array and used str_replace on the final result as follows:

PHP Code:
$data $_GET["songid"];
echo 
"$data<br/><br/>";

$variations = array('- '' - '' -',  ' / ''/ '' /''/');
$replacements = array('-''-''-',  '-''-','-''-' );
$alldata str_replace($variations$replacements$data); 

echo 
"$alldata<br/><br/>";

list(
$artist$title) = split('[-]'$alldata);
echo 
"artist: \"$artist\"<br/>";
echo 
"title: \"$title\"<br/>"
That should account for all instances and seems to work, it's a little slow however, would anyone have an alternative option?

Thanks,
Andy
__________________

Please login or register to view this content. Registration is FREE
- THE Harry Potter Resource!
andypugh is offline
Reply With Quote
View Public Profile
 
Old 03-24-2009, 09:33 PM Re: str_replace & split
design2code's Avatar
Novice Talker

Posts: 9
Trades: 0
I don't think what you've done will make it noticeably slower. This is how I would have done it, so as long as it works I don't think your users are going to complain :P
design2code is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to str_replace & split
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17823 seconds with 12 queries