Jump to content

judeddokoinu

Members
  • Posts

    19
  • Joined

  • Last visited

    Never

Everything posted by judeddokoinu

  1. Well, thanks for the help guys.  I was banging my head on the desk at that one.  I was about to use an explode and loop through and well, that way was much easier.  I didn't even think of removing the extension like that.
  2. I have a user upload script which has the following statement: [code]$file = str_replace('.', '_', $file);[/code] Which replaces dots in the filename with underscores BUT I would like to leave the last dot (the one before the extension) since I do not know beforehand how many characters the file extension will have.  Thus, I can not simply count back 3 characters and add a dot after replacing them. The only thing I can think of is to do the str_replace(), and then come back and replace the last underscore found in the string with a dot, but I'm not entirely sure how to replace the last one. Is there a method to count how many dots are in the filename string, subtract 1 and then replace all but the last? What's the solution here? Thanks. :D **EDIT** Just tried out: [code] $numberdots = substr_count($file, '.') - 1; $file = preg_replace('/./', '_', $file, $numberdots); [/code] But that did not work either.  My filename went from: full.logo.bar_copy.jpg to __ll.logo.bar_copy.jpg which doesn't make any sense to me, but still got dots in it lol.
  3. [quote author=c4onastick link=topic=119478.msg489419#msg489419 date=1166680707]If that's what your data looks like and nl2br doesn't work give this a shot: [code]$htmized_text = preg_replace('/(\r\n)/', '<br \>\1', $source);[/code] [/quote] that preg_replace still added a line break in the saved data.  The output on-screen looks fine, but i need it converted to a single line when I place it in the database.
  4. Alas, it does not remove the \r as well. [code] $newpostbody = nl2br($newpostbody); [/code] I suppose I may have to stick with my longer str_replace. And the reason I'm running it on the string and not directly on the $_POST is because I have several other replacement filters like to change <tab> into several nbsp's and such.
  5. c4, your code is a lot shorter than mine - I might have to try it instead.  I do hope it replaces the \r in front of my \n as well
  6. Ah, very interesting.  I was getting desperate, and found that if I use double quotes instead of singles, it worked fine.  I did have to add the \r in order to get it to save to the database correctly, though, but all is well. solution: [code] $newpostbody = str_replace("\r\n", "<br />", $newpostbody); [/code]
  7. Suppose you have a form with a <textarea>.  When you submit the form, the php will assign the value of whatever is stored in the textarea to a variable.  How do you detect where the user had pressed the enter/return key at? I'd like to be able to str_replace() the newlines/carriage returns in the string with html <br />'s but can't seem to determine how to detect their presence. I currently am trying the following, and have tried all the variations of the first value \n, \n\r, \r, \r\n, but no luck in getting it detected and replaced. [code] $newpostbody = str_replace('\n', '<br />', $newpostbody); [/code]
  8. I'm just looking to trim down my code a bit, if possible. I was wondering if PHP itself had a function that does what this one that I wrote does: [code] function leadzero($number) {         if ($number < 10)         {                 $new_number = '0' . $number;                 return $new_number;         }         else         {                 return $number;         } } [/code]
  9. I tried that out, but it wasn't what I was looking for. I finally decided to just use multiple single-dimensional arrays, and it's working fine that way. After reading your post and playing around with the code, I think what I was trying to do was not working - like, could never work, and so I decided to take the easiest detour instead. Thanks for the help, though.
  10. Not sure about the error, but you do have an extra "=" in this line: [code]<form action="<?=$_SERVER['PHP_SELF']?>"[/code] Should be: [code]<form action="<? $_SERVER['PHP_SELF'] ?>"[/code]
  11. $options is an array? Do something like: [code] <?php // do a loop on this, most likely... $options[0] = $_POST['option1']; $options[1] = $_POST['option2']; // get the values and store as a string. foreach ( $options AS $key=>$value ) {         $new_options .= 'Something: '.$value.' &nbsp;'; } ?> [/code]
  12. I have an array that I create early in my PHP script as a single dimensional array. It contains integers related to years that it gathers from input. Later on in the script, I decided that I'd like to add onto this array with a dimension for months, and a dimension for days. I'll also (later) be adding another dimension containing the data I need. But the problem I'm having is that the first dimension's data is getting overwritten when I attempt to set the second or third dimension's value. [code] for($i=0;$i<$somenum;$i++) { //do something to get the year $myarray[$i] = $year; } [/code] ^-- Results in --v [code] Array ( [0] => 1995 [1] => 1997 [2] => 2002 [3] => 2003 [4] => 2004 [5] => 2005 [6] => 2006 ) [/code] Then, later in the program, I revisit the array, and attempt to set another dimension... [code] for($i=0;$i<$somenum;$i++) { //do something to get the month $myarray[$x][$i] = $month; } for($i=0;$i<$somenum;$i++) { //do something to get the day $myarray[$x][$y][$i] = $day; } [/code] Let's say that I set $day = 09... When I try to set the day? $myarray[$x][$y][$i] = $day; ... I get: [code] Array ( [0] => 0995 [1] => 1997 [2] => 2002 [3] => 2003 [4] => 2004 [5] => 2005 [6] => 2006 ) [/code] The year in the [0] position gets it's first two numbers overwritten! And there are no extra dimensions! So... How can I add these dimensions? I'm so stumped!
  13. I've got a script that scrapes a webpage into a variable, and then loops through the content, echoing each line, but it doesn't show anything on the webpage until it is completed with the loop. Is there a way to show the content *as* it is processed? I'd like to get the content to the user faster.
  14. The problem with changing post to get is that the remote server uses the post vars, and not get vars. I don't have the options of changing the remote server's code. The JS option would work, but it seems that the entire page would have to load, then be examined by JS, and then the form could be submitted. I would like it to be silent - invisible... Basically: [code] 1. Get listing 2. Parse into variables 3. If variable == search term     a. submit form     b. halt 4. Show listings 5. Refresh [/code] I've got all of it completed except for 3a
  15. I'll do my best to explain this, forgive me if it is confusing. I have a remote server which contains a page of ever-changing data. On this page are HTML forms next to each entry on the page (about 20 are shown, if I remember correctly) Each HTML form is just a simple: [code] <form method="POST" action="http://foo.com/view.php?id=342452346">      <input type="hidden" name="password" value="yourpass">      <input type="submit" name="submit" value="submit"> </form> [/code] So, I mirrored this page using a fopen() call to the URL. The reason I'm mirroring is that I want it to auto-refresh, which I can do on my own server via PHP. ALL OF THAT IS DONE, by the way. The page works fine. ***What I'm wanting to do now is have the ability for the user(me) to enter a search term, and if one of the entries on the screen contains that term, then tell the browser to submit the form and load the next page.*** Entering the search term is simple. I can get that setup and into a variable, and I can even flag when it finds it. What I DON'T know is how I can "submit" the form - possibly using a redirect, but how do I get the form's fields into the URL to redirect to? Isn't a hidden field not shown in the URL?
  16. I have an array such as the following: [code] $inventory = array(      "Blue Lampshade (US)" => 15,      "Blue Lampshade (JP)" => 18,      "Cushion" => 7 ); [/code] And I want to be able to test if a string I have is contained in the array as an element, then do something, if not, then just keep executing. I have a foreach() loop I'm running through, and want to highlight an item if it is found, if not, then it still needs to do more processing. Thus, it needs to fail silently, when it does. Pseudocode: [code] ....... foreach($k $v){      $item = "Cushion";      .........      if $inventory[$item] exists{           highlight item (i have this code already)      }      .........      rest of code      ........ } [/code] Can I do that? if($inventory[$item]))? Or would this throw an error or warning on my output? And, yes, I know that data should be stored in mysql, but it is not an option for this particular script.
  17. Thank you, goomba, that was what I was looking for!
  18. Now let me throw a wrench in the gears: Suppose the string were HTML, and I were looking for sections of the code... What delimiter would I split it based on? An angle bracket, or maybe a full tag? Currently, I'm using substr() which doesn't work very well, seeing as how the size of the words change from time to time... **EDIT** I think that using explode on "<" will work... Just have to remember to add the < back into it. It will at least help, anyway. Now that I have them split, I will have to find the string inside of the bigger string *sweat* **EDIT** Okay, I now have an array containing all of the elements, since they begin with "<". You suggested foreach(), and reading up on it, it says that it loops through an array, and can match against expressions. This is where the trouble begins, I think.... I have a shorter string, but still don't know what expression to use to determine if a substring is located inside of a larger string.
  19. Hi, I've got a string that's about 5 kilobytes long, so there is lots of things in it. What I need to do is loop through the string and find occurrences of another string, then backtrack and save text before, inside and after the occrrence. EX: Given the string: [code] $haystack = "the quick brown fox jumped over the fence. the quick brown fox jumped over the hill. the quick brown fox jumped over the hole."; [/code] And I wanted to find the entire sentence that contained the word "hill". [code] $needle = "hill"; [/code] How would I get the output: [code] $pincushion = "the quick brown fox jumped over the hill."; [/code] And assume that the first portion of the sentence is not always the same. Some days it will be: [code] $pincushion = "the slow red hen jumped over the hill."; [/code] I also need to preserve the original string, as there will be multiple searches made. I do know a bit about PHP, but I'm fairly new to the finer points of string functions. Any help would be appreciated.
×
×
  • 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.