-Karl- Posted May 12, 2010 Share Posted May 12, 2010 Okay, so I have this code: // Attack $startatt = strpos($content,'alt="Attack"/>')+14; $endatt = strpos($content,'<a id="skill2"',$startatt); $attack = substr($content,$startatt,$endatt-$startatt); // Defence $startdef = strpos($content,'alt="Defence"/>')+15; $enddef = strpos($content,'<a id="skill3"',$startdef); $defence = substr($content,$startdef,$enddef-$startdef); There's around 23 more of them, is there any way I could create a loop to do this, instead of writing them one by one? Something similar to this: string[] skills = {Attack,Defence,Strength,Ranged,Prayer,Magic,Runecrafting,Construction,Constitution, Agility,Herblore,Thieving,Crafting,Fletching,Slayer,Hunter,Mining, Smithing,Fishing, Cooking,Firemaking,Woodcutting,Farming,Summoning,Dungeoneering} int[] StrPos = {14,15,16,20,14,14,13,15,19,17,15,18,16,16,14,16,14,15,20,14,20,17,21} int[] level = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24} string[] abbre = {att,def,str,ran,pra,mag,run,cons,con,agi,her,thi,cra,fle,sla,hun,smi,fis,coo,fir,woo,far,sum,dun} for(int i = 0;i skills.length;i++) { $start+abbre[i] = strpos($content,'alt="+skills[i]"/>')+StrPos[i]; $end+abbre[i] = strpos($content,'<a id="+level[i]"',$start); $+skills = substr($content,$start+abbre[i],$end+abbre[i]-$start+abbre[i]); } If so, how? Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/ Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 What are you trying to do with this code. If you tell us that, we might be able to help. Ken Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056904 Share on other sites More sharing options...
-Karl- Posted May 12, 2010 Author Share Posted May 12, 2010 Basically, I'm using curl to grab data from an external source, to which I have consent to use. The only method I've found to work is the strpos function. Is there another method which would be more effective? Such as utilizing substr? $testatt = substr($content,'<img src="http://www.runescape.com/img/hiscores/skill_icon_Attack1.gif" alt="Attack"/>', -2); Obviously that won't work as I'm not sure how to define a starting position using substr. Anyway, once the data has been grabbed, it's then stored in a database. Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056911 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 Give us an example of the string you're trying to parse and what results you want. Ken Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056912 Share on other sites More sharing options...
-Karl- Posted May 12, 2010 Author Share Posted May 12, 2010 $ch = curl_init() or die(curl_error()); curl_setopt($ch, CURLOPT_URL,'http://services.runescape.com/m=adventurers-log/display_player_profile.ws?searchName=lnsult'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $raw = curl_exec($ch) or die(curl_error()); curl_close($ch); $content = str_replace($newlines, "", html_entity_decode($raw)); // Attack $startatt = strpos($content,'alt="Attack"/>')+14; $endatt = strpos($content,'<a id="skill2"',$startatt); $attack = substr($content,$startatt,$endatt-$startatt); $attack would output the players level. It all works fine, but I'm just looking for a more effective way to do it. Is there a right(string,2) such as in mSL, the substr looks similar, but I'm unsure if it will produce the same result. Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056913 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 It looks like you should be able to use regular expression matching here, but I'm not good in figuring them out, so I can't help you anymore right now. If I figure it out, I will post my solution. Ken Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056921 Share on other sites More sharing options...
-Karl- Posted May 12, 2010 Author Share Posted May 12, 2010 I cannot use ReGeX as each part is slightly different. There's a good function in mSL and ASP I believe, which is right(string,N) which will select the last 2 characters of a string. Is there an equivalent in PHP, if so how would I utilize it in my script? Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056925 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 If I am understanding your question correctly and looking at the source returned by the curl function, you're trying to parse the following lines: <img src="http://www.runescape.com/img/hiscores/skill_icon_Attack1.gif" alt="Attack"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Defence1.gif" alt="Defence"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Strength1.gif" alt="Strength"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Constitution1.gif" alt="Constitution"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Ranged1.gif" alt="Ranged"/>92 <img src="http://www.runescape.com/img/hiscores/skill_icon_Prayer1.gif" alt="Prayer"/>80 <img src="http://www.runescape.com/img/hiscores/skill_icon_Magic1.gif" alt="Magic"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Cooking1.gif" alt="Cooking"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Woodcutting1.gif" alt="Woodcutting"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Fletching1.gif" alt="Fletching"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Fishing1.gif" alt="Fishing"/>99 <img src="http://www.runescape.com/img/hiscores/skill_icon_Firemaking1.gif" alt="Firemaking"/>86 <img src="http://www.runescape.com/img/hiscores/skill_icon_Crafting1.gif" alt="Crafting"/>87 <img src="http://www.runescape.com/img/hiscores/skill_icon_Smithing1.gif" alt="Smithing"/>83 <img src="http://www.runescape.com/img/hiscores/skill_icon_Mining1.gif" alt="Mining"/>85 <img src="http://www.runescape.com/img/hiscores/skill_icon_Herblore1.gif" alt="Herblore"/>81 <img src="http://www.runescape.com/img/hiscores/skill_icon_Agility1.gif" alt="Agility"/>75 <img src="http://www.runescape.com/img/hiscores/skill_icon_Thieving1.gif" alt="Thieving"/>76 <img src="http://www.runescape.com/img/hiscores/skill_icon_Slayer1.gif" alt="Slayer"/>81 <img src="http://www.runescape.com/img/hiscores/skill_icon_Farming1.gif" alt="Farming"/>83 <img src="http://www.runescape.com/img/hiscores/skill_icon_Runecrafting1.gif" alt="Runecrafting"/>72 <img src="http://www.runescape.com/img/hiscores/skill_icon_Hunter1.gif" alt="Hunter"/>70 <img src="http://www.runescape.com/img/hiscores/skill_icon_Construction1.gif" alt="Construction"/>70 <img src="http://www.runescape.com/img/hiscores/skill_icon_Summoning1.gif" alt="Summoning"/>1 <img src="http://www.runescape.com/img/hiscores/skill_icon_Dungeoneering1.gif" alt="Dungeoneering"/>9 to get the string in the "alt" attribute and the number following the tag. All those lines are similar enough that a regular expression should be able to work. I suggest you post a question in the regular expression subsection and ask how to do it. Ken Link to comment https://forums.phpfreaks.com/topic/201454-shortening-this-code/#findComment-1056931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.