Jump to content

iLuke

Members
  • Posts

    33
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

iLuke's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi guys, Just a quick one - this is more of a bit of a challenge than a problem! I'm pretty good with PHP - I'm definitely not a beginner, but I've come across a problem I've not yet been able to solve satisfactorily. Essentially, I am trying to write a really good, solid OOP library that I can hand out for people to use for CMS-driven websites, and I want to literally just upload some files and have access to some variables... simple enough. But, my snagging point came when I was writing the image library, which handles uploading files and moving them from place to place. These functions as you know usually require an absolute server path - not a relative path and certainly not a URL, so I wanted to place something in my config file that gets the base URL, puts the relative path in one variable (e.g. mysite.com/directory would give /directory) and the server path in another (e.g. mysite.com/directory might give var/www/mysite/www/directory). Now I might just be being foolish and overlooking something, but I've used HTTP_HOST, DOCUMENT_ROOT etc to get this information, but I only want to get the base directory that the application resides in - usually with installed software sure as WordPress you have to specify which directory the files are going to sit in, which I wondered if you could gauge automatically? Any help would be appreciated - I've tried as many clever approaches as I could think of, and the only one I can think of now is setting the variables based on, say, installtion.php since I know the name of the file and where it resides, I can strip out all folders minus the filename, which would give me the root directory, but that seems a little fiddly to me? Someone must have done this before and have a nice clean solution to the problem? I'm at a loss anyway, and I figured it might be a fair challenge to do? Thanks, Luke
  2. Ah excellent stuff - yours is a more efficient way of going about in terms of logic even if the code is a bit heftier - I'd not thought of running some code against the text outside of regular expressions to be honest hah! I am using jQuery on this as well client-side when the code is written, but this is just intended as a catch-net for if that fails (it failed a few times so I was prompted to try this method). I'm surprised there's no one who has tried to do this previously (or at least not that I've found to be easily searchable) - it seems like a fairly common thing to want to do. Anyhow, thank you very much for your time mate - much appreciated. I'm still fairly new to regular expressions and I'm still in awe at how powerful they are (and how damned complex at times!). Thanks again, Luke.
  3. Hi guys! Got a bit of a challenge here - I'm trying to do something with regular expressions that sounds fairly easy: I want to root through a big chunk of text, find all the links, and add a target='_blank' to any links where the domain isn't the same as my site. I'm basically running this function alongside a blog, so that the user never has to worry about manually adding a link target - all external links open in new tabs. The code I've come up with so far is as follows (it's running through OOP, but that shouldn't matter I don't think): This isn't working at all - but it's not giving any errors either, and I've been using an online regular expression checker to make sure there's no errors returned. $str = preg_replace( '/\<a href="((?:http:\/\/)?(?:www\.)?(?![b]MYDOMAIN[/b]).*)"\>([a-zA-Z0-9-_#\.]*)\<\/a\>/i', '<a href="$1" target="_blank">$2</a>', $str ); I've already learned a whole load trying to get this working - negative lookahead assertions, backreferencing, ignoring variables using the ?: notation etc. I never knew regex could get so confusing! I just wondered if anyone out there might know how this could be done? The output that the function will search is raw HTML, and will have had addSlashes applied for security, but I can easily reverse this if that will cause an issue. Thanks so much in advance! I hope this isn't too tricky for someone who knows what they're doing, but I'm stumped! - Luke
  4. Well then thank you very very much for your help! I've learned a lot doing this stuff... it's a pain in the neck if I'm honest, but it sure is useful! The tutorials on the net just aren't really up to standard... it's more of a copy/paste operation from what I've seen. Anyways, thanks again! Luke.
  5. Issue sorted! Somehow it was that problem I described above. No clue how that works considering the data that was there by default was never posted. In any case, it was a browser issue, and that's now sorted =] Thanks again guys!
  6. Well, it seems to have the error on two browsers, but I haven't tried other computers. I do the ob_start() thing out of habit.. If I'm perfectly honest I don't even know what it does lol! A friend just told me I needed it once, so I have added it ever since. I've removed that ob_start() now and it is still busted... dunno if this makes a difference, but, oddly, when I come to log in the first time, the fields are indented (like some default styling for text fields), and has the username "anonymous" and some password. I've never logged in with those details, so I dunno how it's managed to do that, but I change those details to my own and then log in, but then it breaks as it did before. When I come to log in the second time (after which everything works just fine) those details (anonymous etc) aren't already in there. Wondering whether that's the problem? I can't see it, as I'm not actually logging in with the details that are in there by default, but you never know. Thanks anyway for your help thus far!
  7. I gave that a go, by trying to group the two bits of regex I have to find the weapon currently, but that didn't work... and embarrassingly, I'm not sure how you tell regex that spaces are okay too =[ Gonna do some googling, though, and hopefully that'll be the whole thing nailed! Thank you very much for your help! Couldn't have done it without you guys! (Google is terrible.. not one site had that /s thing that you did, and all suggested doing string replaces and all sorts!) Anyhow, thanks again! Luke.
  8. Hi guys! My login/register script works fine, no problems! The odd thing is with this code: <?php session_start(); ob_start(); ini_set('display_errors','On'); error_reporting(E_ALL | E_STRICT); date_default_timezone_set("Europe/London"); $errorMessage = ''; $successfulPost = 0; $host = 'localhost'; $username = 'USER'; $password = 'PASS'; $database = 'DB'; $siteName = "Luke's KoC Scripts"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><?php echo $siteName; ?> ยป Sab Logger</title> </head> <body style="font-family: Verdana;" onload="document.sabPost.sabData.focus();"> <?php if (!isset($_SESSION['LS_KoC_user']) || !isset($_SESSION['LS_KoC_sessionID'])) { $errorMessage = "You are not logged in!"; } else { $connect = mysql_connect($host,$username,$password) or die('Unable to connect to the database server at this time.'); mysql_select_db($database,$connect) or die('Unable to connect to the database at this time.'); $kocUser = $_SESSION['LS_KoC_user']; $sessionID = mysql_query("SELECT sessionID FROM users WHERE username='".$kocUser."' LIMIT 1"); $sessionID = mysql_fetch_assoc($sessionID); $sessionID = $sessionID['sessionID']; if ($sessionID != $_SESSION['LS_KoC_sessionID']) { $errorMessage = "Your session ID does not match that which is held in the database."; } else { if (isset($_POST['sabData'])) { $sabData = $_POST['sabData']; } if (isset($_POST['submitX1']) || isset($_POST['submitX2'])) { $sabData = stripslashes($sabData); echo $sabData; $data = "/.*? dispatches ([0-9]+) .*? sabotage ([0-9]+) of ([a-zA-Z-0-9\_\-]+)'s .*" ; $data .="([A-Z]{1}[a-zA-Z0-9]+) ([A-Z]{1}[a-zA-Z0-9]*)\."; $data .= ".* enter ([a-zA-Z-0-9\_\-]+)'s .*"; $data .= "destroy ([0-9]+) of the enemy's ([A-Z]{1}[a-zA-Z0-9]+)/s"; preg_match($data,$sabData,$intel); echo ' <form name="counter" style="font-size: 9px;">You will be taken back to the input screen in <input style="border: 0px; font-size: 14px; font-weight: bold; width: 11px; margin-left: 2px;" type="text" size="8" name="d2"> seconds.</form> <script> var milisec=0 var seconds=7 document.counter.d2.value="6" function display(){ if (milisec<=0){ milisec=9 seconds=seconds-1 } if (seconds<=-1){ milisec=0 seconds+=1 } else if (seconds==0) { window.location = "http://www.lmbd.co.uk/koc/sabScript.php" } else { milisec-=1 document.counter.d2.value=seconds setTimeout("display()",100) } } display() </script> '; if (isset($_POST['submitX1'])) { $totalSabbed = (1000000 * $intel[7]); echo "<h1> Sabbotage Successfully Logged!</h1>"; echo "<h3> View sab report below: </h3>"; echo "<b>Target:</b> " . $intel[3]; echo "<br />"; echo "<b>Weapon Sabbed:</b> " . $intel[4] . " " . $intel[5]; echo "<br />"; echo "<b>Amount Sabbed:</b> " . $intel[7]; echo "<br />"; echo "<b>Total Sab Value:</b> " . $totalSabbed; } if (isset($_POST['submitX2'])) { $totalSabbed = (1000000 * $intel[7]) * 2; echo "<h1> Sabbotage Successfully Logged!</h1>"; echo "<h3> View sab report below: </h3>"; echo "<b>Sabbotage Target:</b> " . $intel[3]; echo "<br />"; echo "<b>Weapon Targeted:</b> " . $intel[4] . " " . $intel[5]; echo "<br />"; echo "<b>Number Targeted:</b> " . $intel[7]; echo "<br />"; echo "<b>Total Gold Sabbed:</b> " . $totalSabbed . " <i>(" . $totalSabbed/2 . " gold per sabbotage)</i>"; } } else { echo " <fieldset><form action='" . $_SERVER['PHP_SELF'] . "' method='post' name='sabPost' id='sabPost'> <h3>Paste sab data below:</h3> <textarea cols='75' rows='5' name='sabData' id='sabData' onKeyup='document.sabPost.submitX2.focus()'></textarea><br /> <span style='font-size: 10px;'><b>E.g.</b> <i>Your Chief of Intelligence dispatches 1 spies to attempt to sabotage 16 of Remco-MOD's weapons of type Lookout Tower.<br /> Your spies successfully enter Remco-MOD's armory undetected, and destroy 16 of the enemy's Lookout Tower stockpile. Your spies all return safely to your camp. </i></span><br /> <br /> <input type='submit' name='submitX2' value='Submit -- X2' /> <input type='submit' name='submitX1' value='Submit -- X1' /> </form></fieldset> "; } } } ?> <?php if (strlen($errorMessage) != 0) { echo "<h1>".$errorMessage."</h1>"; } elseif ($successfulPost == 1) { echo "<h1>Sab logged successfully!</h1>"; } ?> </body> </html> Now, let's assume you enter this string: The regex works, everything is displayed just fine, and after the JS has done it's thing it takes you back to the form again. But.. on the first time it runs, it tells you that you're not logged in :S So it seems to be destroying the session somehow. Once you log in again, though, you can run the script over and over, and it works just fine. Just wondering how this could be the case and how to fix. Thanks, Luke.
  9. That worked perfectly! Thanks! I'd tried using str_replace to get rid of the linebreaks, but that failed. This works fine! Thanks again! ... One last thing then. Is there a way of making the weapon type take on one array slot... even if it's one word in length or two? At the minute I am doing: echo "<b>Weapon Sabbed:</b> " . $intel[4] . " " . $intel[5]; Which is fine for weapons such as Lookout Tower or Blackpowder Missile etc. But some weapons can be Shield, Nunchaku etc. Obviously the script will break when that is the case and I'm unsure how to make $intel[4] hold both Shield and Lookout Tower, so I can change my echo to echo "<b>Weapon Sabbed:</b> " . $intel[4] ; And it'll work =] Thanks again!
  10. Okay, now I'm a stuck again. Same problem, but this is odd. The raw string I input does have a linebreak after "weapons of type Lookout Tower.", when I paste it into the text box. However, when I echo it out after the form is submitted, that linebreak disappears, and the regex breaks (lots of undefined offset errors). What's strange, though, is that if I manually delete the linebreak from the text box, the code works... even though the string that is being echoed out is exactly the same as the one as was echoed when the linebreak was left in... so how can the regex possibly break? E.g. Echoes: And the regex breaks (Undefined offset: 3 in.. etc) Whereas the string: Echoes: .... And the regex works! But the string that is held in the $sabData variable (which is what is echoed out both times) is exactly the same! So I can't see why it'd break!
  11. Well I did some more work on it, and I've almost got it working! if (isset($_POST['submitX1']) || isset($_POST['submitX2'])) { $sabData = stripslashes($sabData); $data = "/.*? dispatches ([0-9]+) .*? sabotage ([0-9]+) of ([a-zA-Z-0-9\_\-]+)'s .*" ; $data .="([A-Z]{1}[a-zA-Z0-9]+) ([A-Z]{1}[a-zA-Z0-9]*)\."; $data .= ".* enter ([a-zA-Z-0-9\_\-]+)'s .*"; $data .= "destroy ([0-9]+) of the enemy's ([A-Z]{1}[a-zA-Z0-9]+)/"; preg_match($data,$sabData,$intel); if (isset($_POST['submitX1'])) { $totalSabbed = (1000000 * $intel[7]); echo "<h1> Sabbotage Successfully Logged!</h1>"; echo "<h3> View sab report below: </h3>"; echo "<b>Target:</b> " . $intel[3]; echo "<br />"; echo "<b>Weapon Sabbed:</b> " . $intel[4] . " " . $intel[5]; echo "<br />"; echo "<b>Amount Sabbed:</b> " . $intel[7]; echo "<br />"; echo "<b>Total Sab Value:</b> " . $totalSabbed; } It works now IF I remove the line break from the original string after the first instance of the weapon type. I know regex has ways of handling line breaks, and I tried putting in /n \n \/n and even /\n lol, and none of them worked. Also, while I'm sure the little problem described above is just my lack of experience in regex, this one is a little more complex I think... it breaks if I put in Shield as the weapon type, as it only has one word. I dunno why this is, though, as I put a * instead of a + in the code above.. which means zero or more right? So effectively the string reads: One word, beginning with a capital letter, followed by zero or more characters beginning with another capital letter. Dunno why that wont work though? Anyway! Thanks again for your help on this! I'm getting there.. slowly I actually am understanding the string now and what it's all doing, which is how I've managed to figure out why it was breaking before. Oh, and I got rid of the slashes it was adding just using stripslashes... though that didn't work before, but I'm sure not complaning!
  12. Yeah, you answered what I was trying to ask anyway lol. I didn't explain too well I just wish I could at least try to help myself a little with this, but I've got no idea why it wont work. I googled the error I posted, and apparently that's caused because there's nothing stored in the array, which means that the preg_match is failing, but since I'm actually terrible with the regex syntax, I can't see what's wrong with it =[ Sorry to be a pain! .. I've never done regex before.. I've always just copied/pasted a regex string from the net before lol. But this is a little out of the ordinary and needed custom regex.
  13. Nah, I left the regex in there - but I did that bit, so I've probably done something wrong. laffin did tinker with it a bit though, to get the syntax right. But is regex smart enough to know that in the string I want to find, if I put say [0-9] (as well as the other stuff I don#t want) and then in the string I want to be left with e.g. just [0-9].. does regex realise that it's the same [0-9] from the original string, or does it just interpret that as being any string of numbers from zero to nine? laffin said that the array was working for him though, and it was actually echoing out the values that I want, but when I use it, it doesn't seem to work - even though I've not changed anything.
  14. Right, done a little more on it, and now it's giving this error: With the code: if (isset($_POST['submitX2'])) { $data = "/.*? dispatches ([0-9]+) .*? sabotage ([0-9]+) of ([a-zA-Z-0-9\_\-]+)'s .*" ; $data .="type ([A-Z]{1}[a-zA-Z0-9]+)\."; $data .= ".* enter ([a-zA-Z-0-9\_\-]+)'s .*"; $data .= "destroy ([0-9]+) of the enemy's ([A-Z]{1}[a-zA-Z0-9]+)/"; preg_match($data,$sabData,$intel); var_dump($intel); echo $intel[0]; } And the input data in SubmitX2 = At least it's giving an error now!
  15. Okay... that's officially bizarre! Used strip slashes and it still outputs this: When I enter: It's mad.. dunno how it's doing that! I mean, according to the array info you posted, array 2 would be 16 in this case, and instead it's returning the whole string * those few forward slashes. Gah! Thanks again anyway, this is tough stuff!
×
×
  • 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.