Re: [PHP] Replacing 2 strings
On 31/05/05, W Luke <[EMAIL PROTECTED]> wrote: > On 31/05/05, Murray @ PlanetThoughtful <[EMAIL PROTECTED]> wrote: > > > > > > > > function replace($string){ > > > preg_match("/^<\^([a-zA-Z]+?)_([a-zA-Z]+?)>/", $string, $matcharr); > > > $string = str_replace($matcharr[0], $matcharr[1] . " " .$matcharr[2] > > > . ":", $string); > > > return $string; > > > > > > } > > > > > > $string = "<^JIM_JONES> Leicester, 1720. Oxford, 1800 CONFIRMED: meeting > > > at > > > 19.10"; > > > echo replace($string); > > > > > > ?> > > > > > > One of the small benefits of this solution is that Will doesn't need to > > > know > > > what is contained in the target substring beforehand. > > > > I should get into the habit of listing the assumptions my code makes. > > > > In the above example, the following assumptions are present: > > > > - The target substring (in this example, "<^JIM_JONES>") must *always* > > appear at the beginning of the string for the function to perform its task > > > > - The target substring will *always* begin with "<^", will feature a "_" > > between the two name elements, and will conclude with ">" > > > > - There will only be two name elements within the target substring (ie will > > match "<^JIM_JONES>" and "<^MARY_BETH>" but will not match "<^JIM>" or > > "<^MARY_BETH_JONES>") > > > > - The function will only replace the first incidence of the target > > substring. In the eventuality that the target substring value appears > > multiple times in the string being processed, all other instances will be > > left unchanged. > > > > - All other contents of the string being processed can vary without impact > > on the function. > > Thanks Murray, and Brian - both excellent and I'm really grateful for > the help! Clueless when it comes to these types of problems, so > thanks very much, One more question, then I'll leave you alone - promise! How simple would it be to split the two names into 2 vars ($n1 $n2)? -- Will The Corridor of Uncertainty http://www.cricket.mailliw.com/ - Sanity is a madness put to good use - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Replacing 2 strings
On 31/05/05, Murray @ PlanetThoughtful <[EMAIL PROTECTED]> wrote: > > > > > function replace($string){ > > preg_match("/^<\^([a-zA-Z]+?)_([a-zA-Z]+?)>/", $string, $matcharr); > > $string = str_replace($matcharr[0], $matcharr[1] . " " .$matcharr[2] > > . ":", $string); > > return $string; > > > > } > > > > $string = "<^JIM_JONES> Leicester, 1720. Oxford, 1800 CONFIRMED: meeting > > at > > 19.10"; > > echo replace($string); > > > > ?> > > > > One of the small benefits of this solution is that Will doesn't need to > > know > > what is contained in the target substring beforehand. > > I should get into the habit of listing the assumptions my code makes. > > In the above example, the following assumptions are present: > > - The target substring (in this example, "<^JIM_JONES>") must *always* > appear at the beginning of the string for the function to perform its task > > - The target substring will *always* begin with "<^", will feature a "_" > between the two name elements, and will conclude with ">" > > - There will only be two name elements within the target substring (ie will > match "<^JIM_JONES>" and "<^MARY_BETH>" but will not match "<^JIM>" or > "<^MARY_BETH_JONES>") > > - The function will only replace the first incidence of the target > substring. In the eventuality that the target substring value appears > multiple times in the string being processed, all other instances will be > left unchanged. > > - All other contents of the string being processed can vary without impact > on the function. Thanks Murray, and Brian - both excellent and I'm really grateful for the help! Clueless when it comes to these types of problems, so thanks very much, -- Will The Corridor of Uncertainty http://www.cricket.mailliw.com/ - Sanity is a madness put to good use - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
> > function replace($string){ > preg_match("/^<\^([a-zA-Z]+?)_([a-zA-Z]+?)>/", $string, $matcharr); > $string = str_replace($matcharr[0], $matcharr[1] . " " .$matcharr[2] > . ":", $string); > return $string; > > } > > $string = "<^JIM_JONES> Leicester, 1720. Oxford, 1800 CONFIRMED: meeting > at > 19.10"; > echo replace($string); > > ?> > > One of the small benefits of this solution is that Will doesn't need to > know > what is contained in the target substring beforehand. I should get into the habit of listing the assumptions my code makes. In the above example, the following assumptions are present: - The target substring (in this example, "<^JIM_JONES>") must *always* appear at the beginning of the string for the function to perform its task - The target substring will *always* begin with "<^", will feature a "_" between the two name elements, and will conclude with ">" - There will only be two name elements within the target substring (ie will match "<^JIM_JONES>" and "<^MARY_BETH>" but will not match "<^JIM>" or "<^MARY_BETH_JONES>") - The function will only replace the first incidence of the target substring. In the eventuality that the target substring value appears multiple times in the string being processed, all other instances will be left unchanged. - All other contents of the string being processed can vary without impact on the function. Regards, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
> > function replace($string, $search) > { > $string = strstr($string, $search) > $string = preg_replace("/(<|\^|>)/", "",$string); > $string = str_replace("_", " ", $string); > return $string; > > } > > $text = 'My name is <^JIM_JONES> and I like ice cream'; > $search_string = '<^JIM_JONES>'; > echo replace($text, $search_string); > > ?> This is a pretty good solution, however for the sake of paranoia about potentially removing characters that Will may not want targeted by the function (i.e., what if "<", ">", "^" or "_" legitimately appear later in the string and are accidentally removed?), I'd do something like the following: /", $string, $matcharr); $string = str_replace($matcharr[0], $matcharr[1] . " " .$matcharr[2] . ":", $string); return $string; } $string = "<^JIM_JONES> Leicester, 1720. Oxford, 1800 CONFIRMED: meeting at 19.10"; echo replace($string); ?> One of the small benefits of this solution is that Will doesn't need to know what is contained in the target substring beforehand. Regards, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Replacing 2 strings
On Mon, 2005-05-30 at 16:24, W Luke wrote: > On 30/05/05, Brian V Bonini <[EMAIL PROTECTED]> wrote: > > [...] > > > > Again, an example that is as close to your real-world needs as possible > > > would be very helpful. > > > > The original request was: "the text-to-replace is just in a var named > > $text1". > > > > I read that to mean you'd already extracted "<^JIM_JONES>" into $text1 > > Sorry - my mistake/fault. $text1 always begins with <^JIM_JONES> by > is followed by various other stuff: > > <^JIM_JONES> Leicester, 1720. Oxford, 1800 CONFIRMED: meeting at 19.10 > > And I'd like it to read, simply, JIM JONES: (I think having the name > in Caps would be best for now) and leave the rest of the text > unaltered: )/", "",$string); $string = str_replace("_", " ", $string); $string = ucwords(strtolower($string)); $string = str_replace(" ", "-", $string); return $string; } $text = 'My name is <^JIM_JONES> and I like ice cream'; $search_string = '<^JIM_JONES>'; echo replace($text, $search_string); ?> )/", "",$string); $string = str_replace("_", " ", $string); return $string; } $text = 'My name is <^JIM_JONES> and I like ice cream'; $search_string = '<^JIM_JONES>'; echo replace($text, $search_string); ?> -- s/:-[(/]/:-)/g BrianGnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu == gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC Key Info: http://gfx-design.com/keys Linux Registered User #339825 at http://counter.li.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Replacing 2 strings
On 30/05/05, Brian V Bonini <[EMAIL PROTECTED]> wrote: [...] > > Again, an example that is as close to your real-world needs as possible > > would be very helpful. > > The original request was: "the text-to-replace is just in a var named > $text1". > > I read that to mean you'd already extracted "<^JIM_JONES>" into $text1 Sorry - my mistake/fault. $text1 always begins with <^JIM_JONES> by is followed by various other stuff: <^JIM_JONES> Leicester, 1720. Oxford, 1800 CONFIRMED: meeting at 19.10 And I'd like it to read, simply, JIM JONES: (I think having the name in Caps would be best for now) and leave the rest of the text unaltered: JIM JONES: Leicester, 1720. Oxford, 1800 CONFIRMED: meeting at 19.10 Things like "Leicester" need to be capitalised, and CONFIRMED needs to be in caps, so...hope this explains things a bit better. And if you're both too busy, no problem! Thanks, -- Will The Corridor of Uncertainty http://www.cricket.mailliw.com/ - Sanity is a madness put to good use - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
On Mon, 2005-05-30 at 15:24, Murray @ PlanetThoughtful wrote: > > This is a great help, thanks to both. One question I have though. > > How do I just leave the formatting "as is"? In the loop you gave me, > > Brian...: > > [snippage] > > > I can't see how I can disregard strtolower without disrupting the rest > > of the function! My problem is $string contains a whole load of text, > > the formatting of which needs to be retained... > > Hmmm. Methinks your problem is a little more complex than originally > presented. > > Can you give us a pseudo-example of what the string might actually contain? > I think both Brian and I took your original message to mean that your string > would ONLY contain a value like "<^JIM_JONES>", but from this message I get > the sense that the value you're trying to target may be buried in a string > with other text on either side? > > Again, an example that is as close to your real-world needs as possible > would be very helpful. The original request was: "the text-to-replace is just in a var named $text1". I read that to mean you'd already extracted "<^JIM_JONES>" into $text1 )/", "",$string); $string = str_replace("_", " ", $string); $string = ucwords(strtolower($string)); $string = str_replace(" ", "-", $string); return $string; } $text = 'My name is <^JIM_JONES> and I like ice cream'; $search_string = '<^JIM_JONES>'; echo replace($text, $search_string); ?> -- s/:-[(/]/:-)/g BrianGnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu == gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC Key Info: http://gfx-design.com/keys Linux Registered User #339825 at http://counter.li.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
> This is a great help, thanks to both. One question I have though. > How do I just leave the formatting "as is"? In the loop you gave me, > Brian...: [snippage] > I can't see how I can disregard strtolower without disrupting the rest > of the function! My problem is $string contains a whole load of text, > the formatting of which needs to be retained... Hmmm. Methinks your problem is a little more complex than originally presented. Can you give us a pseudo-example of what the string might actually contain? I think both Brian and I took your original message to mean that your string would ONLY contain a value like "<^JIM_JONES>", but from this message I get the sense that the value you're trying to target may be buried in a string with other text on either side? Again, an example that is as close to your real-world needs as possible would be very helpful. Regards, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Replacing 2 strings
On 30/05/05, Brian V Bonini <[EMAIL PROTECTED]> wrote: > On Mon, 2005-05-30 at 11:13, Murray @ PlanetThoughtful wrote: > > > Someone much more clever that I can probably come up with something much > > > cleaner and efficient but This works... > > > > Definitely not more clever and arguably not more efficient, but a different > > way of handling this might be: > > > > > > > function replace($string){ > > $string = preg_replace("/(<|\^|>)/", "",$string); > > $string = str_replace("_", " ", $string); > > $string = ucwords(strtolower($string)); > > $string = str_replace(" ", "-", $string); > > return $string; > > } > > > > echo replace("<^JIM_JONES>"); > > > > ?> > > AHHH! ucwords(); I probably looked right at it a million times.. I knew > there had to be something to do that This is a great help, thanks to both. One question I have though. How do I just leave the formatting "as is"? In the loop you gave me, Brian...: foreach($pieces as $char) { $first_letter[] = $char{0}; $remainder[] = strtolower(substr($char, 1)); } $result = array_merge($first_letter, $remainder); list($frstltr,$lstltr,$frstwrd,$lstwrd) = $result; $string = $frstltr . $frstwrd . " v " . $lstltr . $lstwrd; return $string; } I can't see how I can disregard strtolower without disrupting the rest of the function! My problem is $string contains a whole load of text, the formatting of which needs to be retained... -- Will The Corridor of Uncertainty http://www.cricket.mailliw.com/ - Sanity is a madness put to good use - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
On Mon, 2005-05-30 at 12:58, Murray @ PlanetThoughtful wrote: >$string = "this"; > $string{0} = strtoupper($string{0}); > echo $string; // should return value of "This" > ?> I knew you could access but I didn't realize your could assign/replace specific chars like that, i.e. $string{x} = I can usually work through the logic but am not familiar enough with PHP to know what's available to work with -- s/:-[(/]/:-)/g BrianGnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu == gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC Key Info: http://gfx-design.com/keys Linux Registered User #339825 at http://counter.li.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
> AHHH! ucwords(); I probably looked right at it a million times.. I knew > there had to be something to do that Lol, I know that feeling well! One thing, btw, looking at the solution you provided. Once you'd preg_split()ed the string into component words, you could have simply applied strtoupper() directly to the first character of each word in your foreach loop. As an example: Just thought it was worth mentioning. Regards, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
On Mon, 2005-05-30 at 11:13, Murray @ PlanetThoughtful wrote: > > Someone much more clever that I can probably come up with something much > > cleaner and efficient but This works... > > Definitely not more clever and arguably not more efficient, but a different > way of handling this might be: > > > function replace($string){ > $string = preg_replace("/(<|\^|>)/", "",$string); > $string = str_replace("_", " ", $string); > $string = ucwords(strtolower($string)); > $string = str_replace(" ", "-", $string); > return $string; > } > > echo replace("<^JIM_JONES>"); > > ?> AHHH! ucwords(); I probably looked right at it a million times.. I knew there had to be something to do that -- s/:-[(/]/:-)/g BrianGnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu == gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC Key Info: http://gfx-design.com/keys Linux Registered User #339825 at http://counter.li.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Replacing 2 strings
> Someone much more clever that I can probably come up with something much > cleaner and efficient but This works... Definitely not more clever and arguably not more efficient, but a different way of handling this might be: )/", "",$string); $string = str_replace("_", " ", $string); $string = ucwords(strtolower($string)); $string = str_replace(" ", "-", $string); return $string; } echo replace("<^JIM_JONES>"); ?> Regards, Murray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Replacing 2 strings
On 30/05/05, Brian V Bonini <[EMAIL PROTECTED]> wrote: > On Sun, 2005-05-29 at 12:22, W Luke wrote: > > Hi, > > > > I have some text in a file which, when it's dumped to a var, needs to > > be replaced. In its raw form, it looks like this: <^JIM_JONES> and I > > need to remove the <^_ and > characters and have it read "Jim-Jones" > > > > It's nestled in amongst a load of other text - I'm fopen'ing a file > > and reading it line by line - the text-to-replace is just in a var > > named $text1 > Someone much more clever that I can probably come up with something much > cleaner and efficient but This works... Thanks Brian, that worked a treat. Altered it slightly, but essentially it was just what I needed. Lifesaver, thanks. -- Will The Corridor of Uncertainty http://www.cricket.mailliw.com/ - Sanity is a madness put to good use - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Replacing 2 strings
On Sun, 2005-05-29 at 12:22, W Luke wrote: > Hi, > > I have some text in a file which, when it's dumped to a var, needs to > be replaced. In its raw form, it looks like this: <^JIM_JONES> and I > need to remove the <^_ and > characters and have it read "Jim-Jones" > > It's nestled in amongst a load of other text - I'm fopen'ing a file > and reading it line by line - the text-to-replace is just in a var > named $text1 > > Any ideas would be great > > Will Someone much more clever that I can probably come up with something much cleaner and efficient but This works... "); $string = str_replace($chars, "", $string); $string = str_replace("_", "-", $string); $pieces = preg_split('/-/', $string); foreach($pieces as $char) { $first_letter[] = $char{0}; $remainder[] = strtolower(substr($char, 1)); } $result = array_merge($first_letter, $remainder); list($frstltr,$lstltr,$frstwrd,$lstwrd) = $result; $string = $frstltr . $frstwrd . "-" . $lstltr . $lstwrd; return $string; } $text1 = '<^JIM_JONES>'; echo replace($text1); ?> -- s/:-[(/]/:-)/g BrianGnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu == gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC Key Info: http://gfx-design.com/keys Linux Registered User #339825 at http://counter.li.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Replacing 2 strings
Hi, I have some text in a file which, when it's dumped to a var, needs to be replaced. In its raw form, it looks like this: <^JIM_JONES> and I need to remove the <^_ and > characters and have it read "Jim-Jones" It's nestled in amongst a load of other text - I'm fopen'ing a file and reading it line by line - the text-to-replace is just in a var named $text1 Any ideas would be great Will -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php