Jump to content

dogfighter

Members
  • Posts

    20
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

dogfighter's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm trying to replace words in paragraphs with random words from a database (a-la madlib), but I don't want it to grab partial words. For example, I want it to replace "white" and not part of "whiten" or "whitest". I'm thinking the way to do this is to define individual words as the word preceded by a space and followed by either another space or some form of punctuation...so: " white " " white." " white!" I know this will miss the first word in a paragraph since there's usually not a space before it, but I can live with that. The problem is I don't want to do preg_replace 15 different times just to get all instances. And I also want the replace to use the following character... so a match on " white!" should be replaced with " black!" and not " black ". Here's the code I have so far, mostly borrowed from a free example I found online: <?php function spin($text) { //While there's pairs of square brackets, spin whats inside of them while (!(strpos($text,'[') === FALSE) && !(strpos($text,']') === FALSE)) { //Find the first '[' $leftb = strpos($text,'['); //Find the first ']' $rightb = strpos($text,']'); //Split the string up, then (psudo)randomise the item chosen $spintext = split(',',substr($text,$leftb+1,$rightb-$leftb-1)); $spinselect = trim($spintext[mt_rand(0,count($spintext)-1)]); //Get the whole string to replace including the brackets $brackettext = substr($text,$leftb,($rightb-$leftb)+1); //Replace the [blah,blah2] with whatever item is chosen $text = str_replace($brackettext,$spinselect,$text); } //return the block of modified text return $text; } $tospin = "The quick brown fox."; $patterns[0] = '/ quick /'; $patterns[1] = '/ brown /'; $patterns[2] = '/ fox /'; $replacements[0] = '[ fast , sluggish, speedy ]'; $replacements[1] = '[ pink , white , yellow , blue ]'; $replacements[2] = '[ bear , snake , bird ]'; ksort($patterns); ksort($replacements); $prespin = preg_replace($patterns, $replacements, $tospin); echo spin($prespin); ?> Can anyone help me with this? I'm a bit out of my league with this one..
  2. Here's the output from Live HTTP Headers for loading phpfreaks.com: http://www.phpfreaks.com/ GET / HTTP/1.1 Host: www.phpfreaks.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 GTB5 (.NET CLR 3.5.30729) Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cookie: PHPSESSID=8h13biq1c1g7ralbabq4us3dr3; __utma=29981182.1999759787.1259128904.1259365156.1259371994.4; __utmc=29981182; __utmz=29981182.1259302116.2.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=php%20freaks; __utmb=29981182.3.10.1259371994; phpfreaks_session=4o97q859qfiak5ptkl4n8l77e4 HTTP/1.x 200 OK Date: Sat, 28 Nov 2009 01:35:29 GMT Server: Apache/2.2.14 (EL) X-Powered-By: PHP/5.3.1 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Vary: Accept-Encoding,User-Agent Content-Encoding: gzip Content-Length: 3386 Keep-Alive: timeout=2, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8 ---------------------------------------------------------- http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-8794381281316343&output=html&h=600&slotname=9733596020&w=120&lmt=1259372129&flash=10.0.32&url=http%3A%2F%2Fwww.phpfreaks.com%2F&dt=1259372129950&correlator=1259372129952&frm=0&ga_vid=1999759787.1259128904&ga_sid=1259371994&ga_hid=1720423606&ga_fc=1&u_tz=-480&u_his=3&u_java=1&u_h=1050&u_w=1680&u_ah=1020&u_aw=1680&u_cd=32&u_nplug=25&u_nmime=118&biw=1663&bih=777&fu=0&ifi=1&dtd=40&xpc=nOXf61O8Ww&p=http%3A//www.phpfreaks.com GET /pagead/ads?client=ca-pub-8794381281316343&output=html&h=600&slotname=9733596020&w=120&lmt=1259372129&flash=10.0.32&url=http%3A%2F%2Fwww.phpfreaks.com%2F&dt=1259372129950&correlator=1259372129952&frm=0&ga_vid=1999759787.1259128904&ga_sid=1259371994&ga_hid=1720423606&ga_fc=1&u_tz=-480&u_his=3&u_java=1&u_h=1050&u_w=1680&u_ah=1020&u_aw=1680&u_cd=32&u_nplug=25&u_nmime=118&biw=1663&bih=777&fu=0&ifi=1&dtd=40&xpc=nOXf61O8Ww&p=http%3A//www.phpfreaks.com HTTP/1.1 Host: googleads.g.doubleclick.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 GTB5 (.NET CLR 3.5.30729) Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cookie: id=22b7e6240a000034||t=1258319344|et=730|cs=ik-erpmz HTTP/1.x 200 OK P3P: policyref="http://googleads.g.doubleclick.net/pagead/gcn_p3p_.xml", CP="CURa ADMa DEVa TAIo PSAo PSDo OUR IND UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR" Content-Type: text/html; charset=UTF-8 Content-Encoding: gzip Date: Sat, 28 Nov 2009 01:35:30 GMT Server: cafe Cache-Control: private, x-gzip-ok="" Content-Length: 1890 X-XSS-Protection: 0 ---------------------------------------------------------- http://www.google-analytics.com/__utm.gif?utmwv=4.5.9&utmn=34123934&utmhn=www.phpfreaks.com&utmcs=UTF-8&utmsr=1680x1050&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=10.0%20r32&utmdt=PHP%20Freaks%20-%20Index&utmhid=1720423606&utmr=-&utmp=%2F&utmac=UA-4220376&utmcc=__utma%3D29981182.1999759787.1259128904.1259365156.1259371994.4%3B%2B__utmz%3D29981182.1259302116.2.3.utmcsr%3Dgoogle%7Cutmccn%3D(organic)%7Cutmcmd%3Dorganic%7Cutmctr%3Dphp%2520freaks%3B GET /__utm.gif?utmwv=4.5.9&utmn=34123934&utmhn=www.phpfreaks.com&utmcs=UTF-8&utmsr=1680x1050&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=10.0%20r32&utmdt=PHP%20Freaks%20-%20Index&utmhid=1720423606&utmr=-&utmp=%2F&utmac=UA-4220376&utmcc=__utma%3D29981182.1999759787.1259128904.1259365156.1259371994.4%3B%2B__utmz%3D29981182.1259302116.2.3.utmcsr%3Dgoogle%7Cutmccn%3D(organic)%7Cutmcmd%3Dorganic%7Cutmctr%3Dphp%2520freaks%3B HTTP/1.1 Host: www.google-analytics.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 GTB5 (.NET CLR 3.5.30729) Accept: image/png,image/*;q=0.8,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive HTTP/1.x 200 OK Date: Sat, 28 Nov 2009 01:35:30 GMT Content-Length: 35 Pragma: no-cache Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate Expires: Wed, 19 Apr 2000 11:43:00 GMT Last-Modified: Wed, 21 Jan 2004 19:50:30 GMT Content-Type: image/gif Server: Golfe X-XSS-Protection: 0 ---------------------------------------------------------- http://toolbarqueries.google.com/search?features=Rank:&sourceid=navclient-ff&client=navclient-auto-ff&googleip=O;null;172&iqrn=bzAC&querytime=Vm&orig=0_gX&ch=8c2a175f3&q=info:http%3A%2F%2Fwww.phpfreaks.com%2F GET /search?features=Rank:&sourceid=navclient-ff&client=navclient-auto-ff&googleip=O;null;172&iqrn=bzAC&querytime=Vm&orig=0_gX&ch=8c2a175f3&q=info:http%3A%2F%2Fwww.phpfreaks.com%2F HTTP/1.1 Host: toolbarqueries.google.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 GTB5 (.NET CLR 3.5.30729) GoogleToolbarFF 5.0.20090813 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cookie: PREF=ID=abd8ca2f00165275:U=1ff398d2fd890de0:FF=4:TB=2:LD=en:NR=10:TM=1258319078:LM=1258692057:GM=1:S=Ywj9c5UL_pG8mnNj; NID=29=PPw2E0NsCVRaY2LHMUKw9ixx5kbboq6NjpwmpeEozF5f53wSL6O1G642LGTl97zfqv019RjlMPDsXUkjIqVWPQQAkGAN1CXJLB_2pggHy7oBD1_7ZglzeRS2E00BDYwc; TZ=480; S=static_files=3QQ4kOEUSQ4; SID=DQAAAI8AAABwjVqwbdIvtHPaWhkljOaJpC4PwNwvN-LC0KRmBKE4vB5MU4HGFSykxIrRs25gqg0BD1usUnRd6bV7RjBVFu5Okw3BbpRYf7hy3wPSwFfCVhxqDCXSAGTjK1ezdawPBx1Iu3RqblpvyolyxdOaMNXn8lIaebdMrMHjOir7VHbDQ3GiHA0IbVuvLU2UIcFHWK8; HSID=AAYE1NrZh-WqB7HZs HTTP/1.x 200 OK Cache-Control: private, max-age=0 Date: Sat, 28 Nov 2009 01:35:30 GMT Expires: -1 Content-Type: text/html; charset=ISO-8859-1 Server: gws Transfer-Encoding: chunked Content-Encoding: gzip X-XSS-Protection: 0 ---------------------------------------------------------- Here's the output from get_headers: Array ( [0] => HTTP/1.1 200 OK [1] => Date: Sat, 28 Nov 2009 01:37:49 GMT [2] => Server: Apache/2.2.14 (EL) [3] => X-Powered-By: PHP/5.3.1 [4] => Set-Cookie: phpfreaks_session=9c3pakspalriht09bl42fvjae3; expires=Sun, 28-Nov-2010 01:37:49 GMT; path=/ [5] => Expires: Thu, 19 Nov 1981 08:52:00 GMT [6] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 [7] => Pragma: no-cache [8] => Vary: Accept-Encoding,User-Agent [9] => Connection: close [10] => Content-Type: text/html; charset=UTF-8 ) Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 28 Nov 2009 01:37:49 GMT [server] => Apache/2.2.14 (EL) [X-Powered-By] => PHP/5.3.1 [set-Cookie] => phpfreaks_session=g9hj449k77vod3826d7272u6m4; expires=Sun, 28-Nov-2010 01:37:49 GMT; path=/ [Expires] => Thu, 19 Nov 1981 08:52:00 GMT [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0 [Pragma] => no-cache [Vary] => Accept-Encoding,User-Agent [Connection] => close [Content-Type] => text/html; charset=UTF-8 ) Notice the blocks that are missing? Specifically the ones pertaining to the calls to various google services? I need everything, including those.
  3. get_headers was the first thing I tried, but like I mentioned, it only returns one header in an array.
  4. Hey all, like many of you I use the Firefox addon "Live http Headers". I'm trying to write a tool that will basically do the same thing, but web-based... so the user would enter a URL and the tool would spit out the same output that Live http Headers would have shown if you were capturing them using it. Problem is, sometimes the output from Live http Headers contains many requests of different types, for different things, on different servers. The PHP code I've been able to find in the manual pertaining to headers really only returns one (the last one). What function should I be looking at if I want it to spit out ALL of the headers and requests, a-la the Firefox addon?
  5. I have a script that includes mysqlclass.php which will not connect to my database for some reason. Here is the script up to the line where it dies: <?php include 'mysqlclass.php'; $server = "localhost"; $user = "mydatabase_admin"; $pass = "swordfish"; $database = "mydatabase_name"; $mysql = new MySQL; $mysql->config( $server, $user, $pass, $database ); $mysql->connect() or die("error" . mysql_error()); When I run it, it just prints "error" and does not include any mysql_error. Here is mysqlclass.php: <?php class mysql { function close(){ return @mysql_close($this->conn); } function config( $server, $user, $pass, $database ){ $this->database = $database; $this->pass = $pass; $this->server = $server; $this->user = $user; return true; } function connect(){ $this->conn = mysql_connect($this->server, $this->user, $this->pass) or die ("Unable to connect to Database Server"); @mysql_select_db( $this->database, $this->conn ) or die ("Could not select database"); } function get_results(){ return @mysql_fetch_array( $this->query ); } function get_row(){ return mysql_fetch_row( $this->query ); } function query( $sql ){ return $this->query = mysql_query($sql, $this->conn); } function select_db(){ mysql_select_db( $this->db_name ) or $this->report_error('Error selecting MySQL database. Database name: '.$this->db_name."\r\n"); } } ?> I didn't write the script I'm just trying to fix it... any ideas what's killing it?
  6. Yeah it's actually for song lyrics, swaps out words. Sounds lame but the output will be pretty entertaining If it ever works
  7. Matt-- I've actually got those as root words in my DB, so don't could become do not, for example
  8. Thanks for the tip. I can explode the replacements and call them at random using shuffle, but I'm still miles away from anything resembling a solution. :-\
  9. I may have bitten off a bit more than I can chew this time. I have a string, and a database that's like a thesaurus. Column 1 is the root word and column 2 is a comma-separated list of words that it could be replaced with. Like this: ROOT REPLACEMENTS rain rain,snow,hail bird bird,cow,giraffe Lists of replacement "madlib" words are separated by commas, with no spaces. When I run the script, I want it to take the text in $sentence, find each root word, and replace that word with a random corresponding synonym. So for the string $sentence, <?php $sentence = "The rain in Spain stays mainly on the plain. A bird in the hand is worth two in the bush! Big bird!"; ?> And when I run the script, I would like it to find all instances the first root word from the DB ("rain") in $sentence and replace them with random corresponding replacement words ("rain", "snow" or "hail"). Then find the second root word ("bird") in $sentence and replace it with random corresponding replacement words ("bird", "cow" or "giraffe"). On and on until it goes through the whole database, which isn't very big anyway. When it's all done with this, I just want it to output the final result. So maybe the first "bird" becomes "cow" and the second "bird" becomes "giraffe". Or some other random combination. Every time my visitor hits refresh, whatever string they entered ($sentence) should be madlibbed using the root words in my database and random corresponding replacement words. I've posted what I have so far below, but at this point I'm stuck. For one thing, I can't just match " bird " because it won't always be surrounded by spaces (like in the third sentence). Sometimes it will be surrounded by a space and a period, a space and an exclamation point, a pair of double quotes, double quotes and a space, etc. Also, even when I do match that way, the first "bird" is always replaced by the same word that replaces the second "bird". I've been plugging away at this for two days now (hey, I'm a newbie) and I just feel like I'm getting nowhere. <?php // Omitted the part where I connect to the DB $var1 = mysql_query("SELECT `root` FROM `list`") or die (mysql_error()); // Select all root words from the madlib DB $sentence = "The rain in Spain stays mainly on the plain. A bird in the hand is worth two in the bush!"; //This is the string I want to match words in and replace. while($dbword = mysql_fetch_array($var1)){ $dbsyns = mysql_query("SELECT `synonyms` FROM `list` WHERE `root` = '".addslashes($dbword['root'])."'") or die (mysql_error()); // Get the comma-separated synonyms from the DB for the first root word $synonyms = mysql_fetch_array($dbsyns); $replacements = explode(",", $synonyms[0]); // Explode comma-separated synonyms to create array of synonyms $replaceshuffle = shuffle($replacements); // Shuffle the synonyms } ?>
  10. I've put the HTML document into a variable ($html) and now I want to pull out all images and display them in order. I tried using xpath but it was only returning the img src and not the height or width. I figure I need to match a regular expression that will get all <img src * > and put them in an array, and then recall that array with a loop. But I can't say I know HOW to do that... ???
  11. Worked like a charm! Doesn't split at the exclamation points (joins them with the next sentence) but I can probably work that one out. Thanks Ken (and everyone else!) for your help.
  12. Anything I can throw in there to preserve the ending punctuation the way preg_offset_capture did?
×
×
  • 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.