Jump to content

kyleabaker

Members
  • Posts

    30
  • Joined

  • Last visited

Everything posted by kyleabaker

  1. I'm trying to write regex to take an input string and return results of each anchor tag that is found. For example, in the following string, it should return 3 results: My expected results are: 1. <a href="link1.html" data-modal="sdfdsf87ds87fdsf8bds8fb">string</a> 2. <a href="link2.html">parse</a> 3. <a class="someclass" href="link3.html">some</a> I'm trying to test this at http://regexpal.com/ and the problem I'm seeing is that my regex ( <a (.+)[^<]*</a> ) is selecting everything from the start of the first anchor tag to the end of the last anchor tag and I can't seem to figure out how to split these apart. Any suggestions so it returns each tag as a separate result in the match array? Thanks in advance!
  2. Excellent. Thanks so much for the help...and so quickly! Edit: If you use the code tag it won't remove that code. It seems the php bbcode tag will though.
  3. I've searched all over and can't seem to find anything that seems to work for what I need. I've got preg_replace() working a little, but not as well as I need it to work. What I'm trying to accomplish is to take a string that contains html and convert most of the tags from html tags to bbcode (mainly just the basics; img, a, b, etc.). So for the <img> tag I've got it working a little for replacing basic img tags, but I need it to be dynamic so that if the tags aren't arranged in a specific order then it will still be recognized and parsed. <img src="http://www.example.domain/image.png"> <img src="http://www.example.domain/image.png" alt="blah"> <img id="something" src="http://www.example.domain/image.png" style="styling-here"> These should all be detected and replaced with... [img=http://www.example.domain/image.png] This seems easy enough, but it's giving me trouble. What I have so far is this: $content = preg_replace("/<img src=\"([ a-zA-Z0-9|\/\:\+.%?=_-]*)\">/i", "[img=$1]", $content); That seems to work great for the first image style, but I need it to match the others as well. I know there is a better way of doing this, but that's the cleanest I could make this code. Can someone help me get this working to match all of these images and just place the url for the img in the bbcode tag? Thanks in advanced.
  4. Go a head and post some of your code and I'll see if I can help more.
  5. I threw in a boolean to see if be true if the item was added, if not then it adds to the beginning of the list since it was all shifted down, but I'm still looking for a better way if anyone can help.
  6. Still having a bit of trouble, but I have this one pin-pointed, just don't know how to handle it. Say I have the following number of hits in order that I find them: 1 1 1 1 3 My for loop is correcting shifting the entries for the first 4 down when it gets to a result with more hits, but I can't figure our how to insert it because my conditions aren't 100% correct. So when I run the search and it processes it shifts 4 down, but leaves the first result in position 1 followed by 1st,2nd,3rd,4th.. 1 1 1 1 1 Here is what I've got now.. if (strlen($searchresults[0]) == 0) { $searchhits[0]=$hits; $searchresults[0]=$searchresultdata; echo "(1) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>"; } else { for ($j = count($searchresults)-1; $j >= 0; $j--) { if ($searchhits[$j]<$hits) { $searchhits[$j+1]=$searchhits[$j]; $searchresults[$j+1]=$searchresults[$j]; echo "(2) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>"; } else { $searchhits[$j+1]=$hits; $searchresults[$j+1]=$searchresultdata; echo "(3) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>"; break; } } } and here is what it echoes.. (1) Post ID: 29 - Post Hits: 1 - Array Count: 1 (3) Post ID: 25 - Post Hits: 1 - Array Count: 2 (3) Post ID: 19 - Post Hits: 1 - Array Count: 3 (3) Post ID: 17 - Post Hits: 1 - Array Count: 4 (3) Post ID: 12 - Post Hits: 1 - Array Count: 5 (3) Post ID: 8 - Post Hits: 1 - Array Count: 6 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 Post ID is shifting everything down, but never caught to insert at position 0 in the array and I can't figure out how to catch it without catching all the others where $j is 0. Got any ideas?
  7. haha, lmao. I figured it would be something so simple. So the rest of the logic makes sense? I will try this when I get out of class. Thanks sasa!
  8. If you want the content that you're filtering to change as you pick new or old then you will need either ajax to change it in a container on the page or just reload the page with the settings that are selected. However, I also would need more details as to what you are trying to do..? If you are planning to reload the page when the form is submitted then you could use $HTTP_POST_VARS or $_GET to grab the settings you select and sort the content. You could also set the default selected item in the select box by echoing the options out and adding (I think this is correct) "selected" inside the <option> tag.
  9. First of all, I just want to point out that I'm very new to php and my code may not be the most ideal. I'm trying to make a better search page for my site that I've been slowly improving. I made a custom blog type of site just for fun I guess and I have a search page, but it's not working exactly how I want it to so I've taken up the task of rewriting it. So here is what I'm doing.. I'm splitting the search terms used into an array (works fine) and looping through the blog posts on my site searching for matches. If it finds a match then I add the number of matches for that word to a $hits variable and add the number for the next term and so on until all the words have been searched for that blog post. Then (the part that is giving me trouble) I am trying to loop through an array that I'm using to sort the results as I go according to the number of hits. I've tried several different things, but setting a specific array item doesn't seem to save as I have in my head. I guess I don't understand it well enough to think through it enough and catch the problem in my head, but I do understand how arrays work. Here is what I'm trying to do with the results that I collect.. (my reasoning and explanation is in the code, hopefully it helps get my goal across well enough) //$searchhits -> an array holding # of hits for each result so I can use it to figure out where to place the next results //$searchresults -> of course holds the results in the format that I want to echo out in the end //$hits -> integer number, # of hits for the specific blog post to be added to the arrays //$searchresultdata -> basic content from the blog post that I want to echo out for that result later if (strlen($searchresults[0]) == 0) { //**if the array is blank just add to start and be done with this result $searchhits[0]=$hits; $searchresults[0]=$searchresultdata; } else { //**else loop through the hits array and find the spot that this result should be inserted and shift everything else down for ($j = count($searchresults)-1; $j > -1; $j--) { //starting at bottom so I can shift items down if ($searchhits[$j]<$hits) { //shift item down and clear item's previous spot (just trying NULL to see if it made a different, it didn't) $searchhits[$j+1]=$searchhits[$j]; $searchresults[$j+1]=$searchresults[$j]; $searchhits[$j]=NULL; $searchresults[$j]=NULL; } else { //from bottom up, I'm assuming j+1 is clear since I NULL it if anything was once there and if it was the last spot that was compared then that spot+1 should be empty to add to $searchhits[$j+1]=$hits; $searchresults[$j+1]=$searchresultdata; } } } I'm fairly confident that the problem is hidden (to me) in the "else" inside the for loop, but I can't figure it out in my head why it won't work. I know you guys are gurus here, so this should be a piece of cake, haha. I'm looking for some simple code that I can understand by the way. Thanks in advanced..and sorry if it's an easy thing to fix. I've looked over the php manuals online for a while trying to figure this out. ??? Feel free to simplify my code! Thanks!
  10. haha, which page? all of the ones I've checked pass with flying colors. edit: the forum seems to validate fine, but parts of the rest of the site don't...I take it back until you fix it phpfreaks.
  11. Just did a quick check to see if phpfreaks passes validation. Just wanted to say how glad I am to see such a great site taking advantage of this! It's sad to see how many websites and website owners just don't care about this. Propers for being such a good role model at this! Now we just need to get the rest of the major websites to work on this, hehe. I couldn't believe when I checked php.net that they don't pass 100%. A lot of their pages do pass, however, some don't... http://us2.php.net/split ...for example. Just needs some simple modifications.
  12. So it seems to be sorting the array based off of the 3rd character in the list. ie. news.id.hits.data screenshots.id.hits.data sorts to.. screenshots.id.hits.data news.id.hits.data ..i'm trying to figure out how to sort it by the value of hits, so i need to split by chr(46) and compare values. so i finally fixed it by using this.. <?PHP function sortByHits($l, $r) { $l_exploded = explode(chr(46),$l); $r_exploded = explode(chr(46),$r); return $l_exploded[2] == $r_exploded[2] ? 0 : ($l_exploded[2] > $r_exploded[2] ? -1 : 1); } usort($searchresults, "sortByHits"); ?> thanks so much Glyde for the help! Don't laugh at my terrible variable names, hehe.
  13. What exactly is this line doing... return $l[2] == $r[2] ? 0 : ($l[2] < $r[2] ? -1 : 1); can anyone explain it? Cause I had my search echo out the number of hits for each item and I'm seeing the following list.. 1 1 3 1 1 1 ..and it should be.. 3 1 1 1 1 1 ...maybe it is comparing in the wrong place..? Not sure tho, I just don't understand what that line is checking exactly.
  14. Sorry, i had a small typo in the code, so it is sorting now. It's still not sorting completely correctly, but I'm gonna do some debugging and get back to you all.
  15. Glyde I tested that and changed $resultArray to the array that contains the search results ($searchresults) and here is what it says: Warning: usort() [function.usort]: The argument should be an array in /***********/blah.com/search.php on line 60
  16. Moon-Man.net I'm using MySQL database. I'll post some code in a bit, gonna try what Glyde posted first. Glyde Thanks, gonna test it, I'll be back.
  17. Hey, I'm trying to make my own search page for my website and I have it gathering correct results exactly as it should, but I'd like to find a way to have it sort these results according to their relevance (makes mroe sense of course). As it is now, it is simply pulling them from my database newest-to-oldest. The array elements are compiled like this.. type_of_item.id_#_of_item.hits_for_item.data ..so you can see that I have them all seperated by periods, then to print the data I just get the length that it is from the left and echo it out. I have all of that working, but I need to find a way to sort my array by the 3rd period seperated part...the hits. I want the items with the most hits at the top. Anyone have any ideas or help? Thanks in advanced.
  18. Thanks Glyde! The "." was my only problem, so once I replaced that with chr(46) it started working perfectly! Now I just have to figure out how to sort the array by the tags that I have in them, like the number of hits. So the hits are stored as the 3rd part (type.id.hits.data) so can I have it sort the array that way? Then my search results will make more sense.
  19. Hey all. I'm fairly new to php, been here a few times now for help with great results! But now I'm stuck and I'm not sure exactly what the problem is, but I'll try my best to explain it to you. On my website I'm trying to make a search thru my mySQL database, so it any keywords are found then I'm adding the details to the next item in an array that I create. I've tested the array and it is storing the information into the array properly because it echoes out all of the information correctly. My problem is that, at the beginning of each item in the array I have information that tells me what part of the database it was pulled from and the id number and also how many times the a keyword was found in that specific database entry. So you have something like this.. Picture of the Array{ news.id=1.hits=3.data from the item here comments.id=5.hits=2.data from the item here links.id=7.hits=5.data from the item here blah.id=8.hits=1.data from the item here } My problem is that when I am trying to split the array item [split(".",array,2)] it's not working as I am thinking. I know the problem is obviously something in my code. I'm wanting to basically split the first 3 parts that are divided by "." so I can get length since the id will vary and use the combined lengths + the number of periods to set the left point of substr() then set the right point to be the length of the array item. That should effectively leave the data remaining so I want to print the data out with an echo as the result, but for some reason when I test to see if it is news or comments, etc it never matches for the first split at "."'s....so my results don't echo cause nothing matches. I hope this is somewhat understandable. Any help would be great. I think an item in an array is basically a string, but I'm not sure why it is not handling like I have in my head. I may also be using split incorrectly. I looked at the php manuals but I might still have it wrong. Thanks in advanced!
  20. @genericnumber1 Thanks! I'll give it a shot with throwing them out completely. I don't really care about filtering language on my site cause I'm not worried about foul language, I just have bots posting about meds and other stuff, etc. I just wanna throw out the bot posts. No one else on my site is gonna be posting about viagra, lol. Thanks for the fast response!
  21. Hey all, sorry if this has been posted and explained before. I've searched several times, but I'm just not really even sure what to search for. I'm basically trying to define an array of "bad words" and I want to check string1 and make sure that none of the items in the array are found to occur in string1. I basically want to throw out any string1's that contain any "bad words". By bad words, I mainly mean that I'm trying to filter common words used in bot messages. So I basically don't know what to use in php to check in the string for these bad words. I'm pretty sure that I'll have to use a loop of some sort (as there are no built in functions to my knowledge that automate this type of search). I searched on the php manual site and was unsure of which function to use. I was thinking maybe use "strstr" and on the first occurrence, just throw out the message all together. But I want to make sure that this is a speedy/accurate method of doing what my goal is. I also know that I will need to make the comparisons case insensitive...and I found kind of what I'm looking for here, but am unable to really interpret it completely (still a n00b, lol).. http://fundisom.com/phpsnippets/snip/string_handling/string_in_string/ Thanks all in advanced.
  22. Thanks thorpe. I'll look into it. Also, thanks fert. using the GET command that you helped me with I've halfway accomplished my goal, haha. I'll be back if I get stuck. Thanks all!
×
×
  • 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.