Jump to content

kyleabaker

Members
  • Posts

    30
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

kyleabaker's Achievements

Newbie

Newbie (1/5)

0

Reputation

  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.
×
×
  • 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.