[PHP] string function that adds before and after
What I'm trying to do is find a substring in a much longer string and insert another string before and after the substring. For example: string to find: weather string to search in: This is the worst weather ever. Weather around here is terrible. string to add before: strong string to add after: /strong I can't do a simple search and replace, because I need to maintain the case sensitivity of the word that's in the searched string. So I was hoping to find something that would locate the string to find, add something before and after it (which that something before and after could be different from each other), then continue the search to see if another instance exists. And so on and so forth There's a number of functions in PHP that will give me the position of the *first* instance of the matched string, but it doesn't look like the function would keep searching after the first match. Anyway, am I overlooking a function that already has the functionality that I'm searching for? Or does anyone have any ideas how I can accomplish this efficiently since my search strings can be quite long? Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string function that adds before and after
There's a number of functions in PHP that will give me the position of the *first* instance of the matched string, but it doesn't look like the function would keep searching after the first match. Anyway, am I overlooking a function that already has the functionality that I'm searching for? Or does anyone have any ideas how I can accomplish this efficiently since my search strings can be quite long? try this preg_replace('/(weather)/i', strong$1/strong, 'This is the worst weather ever. Weather around here is terrible. ') -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string function that adds before and after
Matt M. wrote: There's a number of functions in PHP that will give me the position of the *first* instance of the matched string, but it doesn't look like the function would keep searching after the first match. Anyway, am I overlooking a function that already has the functionality that I'm searching for? Or does anyone have any ideas how I can accomplish this efficiently since my search strings can be quite long? try this preg_replace('/(weather)/i', strong$1/strong, 'This is the worst weather ever. Weather around here is terrible. ') Thanks Matt. I think that will do the trick. Let me see if I understand it correctly. the i will make the search case-INsensitive, and the parenthesis around weather will store what it finds in the variable $1? Is that right? Thanks again, that was a big help. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string function that adds before and after
There's a number of functions in PHP that will give me the position of the *first* instance of the matched string, but it doesn't look like the function would keep searching after the first match. Anyway, am I overlooking a function that already has the functionality that I'm searching for? Or does anyone have any ideas how I can accomplish this efficiently since my search strings can be quite long? try this preg_replace('/(weather)/i', strong$1/strong, 'This is the worst weather ever. Weather around here is terrible. ') Thanks Matt. I think that will do the trick. Let me see if I understand it correctly. the i will make the search case-INsensitive, and the parenthesis around weather will store what it finds in the variable $1? Is that right? Also consider str_replace() as it's faster albeit case sensitive. str_ireplace() exists in PHP 5. Just another option, I'm surprised you didn't find it when looking around strpos() and friends. Your assumptions above are correct, sorry Matt for stepping in! :) I prefer cold weather. Regards, Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string function that adds before and after
Philip Olson wrote: There's a number of functions in PHP that will give me the position of the *first* instance of the matched string, but it doesn't look like the function would keep searching after the first match. Anyway, am I overlooking a function that already has the functionality that I'm searching for? Or does anyone have any ideas how I can accomplish this efficiently since my search strings can be quite long? try this preg_replace('/(weather)/i', strong$1/strong, 'This is the worst weather ever. Weather around here is terrible. ') Thanks Matt. I think that will do the trick. Let me see if I understand it correctly. the i will make the search case-INsensitive, and the parenthesis around weather will store what it finds in the variable $1? Is that right? Also consider str_replace() as it's faster albeit case sensitive. str_ireplace() exists in PHP 5. Just another option, I'm surprised you didn't find it when looking around strpos() and friends. Your assumptions above are correct, sorry Matt for stepping in! :) I prefer cold weather. Regards, Philip Thanks Philip. I did notice str_replace() but it didn't look like I could use it without it actually replacing what it found instead of just adding strings before and after it. Am I wrong? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string function that adds before and after
-Original Message- From: Gabe Sent: Friday, June 25, 2004, 9:06:15 PM Philip Olson wrote: There's a number of functions in PHP that will give me the position of the *first* instance of the matched string, but it doesn't look like the function would keep searching after the first match. Anyway, am I overlooking a function that already has the functionality that I'm searching for? Or does anyone have any ideas how I can accomplish this efficiently since my search strings can be quite long? try this preg_replace('/(weather)/i', strong$1/strong, 'This is the worst weather ever. Weather around here is terrible. ') Thanks Matt. I think that will do the trick. Let me see if I understand it correctly. the i will make the search case-INsensitive, and the parenthesis around weather will store what it finds in the variable $1? Is that right? Also consider str_replace() as it's faster albeit case sensitive. str_ireplace() exists in PHP 5. Just another option, I'm surprised you didn't find it when looking around strpos() and friends. Your assumptions above are correct, sorry Matt for stepping in! :) I prefer cold weather. Regards, Philip Thanks Philip. I did notice str_replace() but it didn't look like I could use it without it actually replacing what it found instead of just adding strings before and after it. Am I wrong? I'd say it will do the job just fine if case sensitivity is not an issue: $word = 'weather'; $new = str_replace($word, strong$word/strong, $phrase); ... speaks for itself ;) /rh -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string function that adds before and after
* Thus wrote Richard Harb: -Original Message- From: Gabe Sent: Friday, June 25, 2004, 9:06:15 PM Philip Olson wrote: lots of ... I'd say it will do the job just fine if case sensitivity is not an issue: $word = 'weather'; $new = str_replace($word, strong$word/strong, $phrase); ... speaks for itself ;) I'm thinking to myself at this moment... style weather { font-weight: bold } /style It simply just would make the world a whole lot nicer :) Do note, this is not a valid css tag, just a simple dream of mine. Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] string function that inserts a char
I just finished looking through string functions http://us2.php.net/manual/en/ref.strings.php and can't find one that inserts a character, not replaces one. If there's a string that's over 50 chars long without a space, I want to insert a space without replacing or losing any of the original characters. Is there a function to do that? advance thanks, Dale -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] About php String function
Hello, I am using strpos function for finding the string position in a particular string .If the string value is equal to zero or greater than zero some steps to be executed. If the position is empty it is taking as zero value and executing the steps under equal to zero loop..Is there any method avoid doing that ?? Regards, Uma -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] About php String function
Hi, Thursday, September 25, 2003, 3:18:19 PM, you wrote: UST Hello, UST I am using strpos function for finding the string position in a UST particular string .If the string value is equal to zero or greater UST than zero some steps to be executed. If the position is empty it is taking UST as zero value and executing the steps under equal to zero loop..Is there UST any method avoid doing that ?? UST Regards, UST Uma You need to use if ($pos !== false) { //pos found } -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] About php String function
On Thu, Sep 25, 2003 at 10:48:19AM +0530, Uma Shankari T. wrote: : : I am using strpos function for finding the string position in a : particular string .If the string value is equal to zero or greater : than zero some steps to be executed. If the position is empty it is taking : as zero value and executing the steps under equal to zero loop..Is there : any method avoid doing that ?? The online manual for strpos() documents this: http://www.php.net/manual/en/function.strpos.php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // because the position of 'a' was the 0th (first) character. if ($pos === false) { echo The string '$findme' was not found in the string '$mystring'; } else { echo The string '$findme' was found in the string '$mystring'; echo and exists at position $pos; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] String function
Hi, Please can someone tell me how i can tell if a string contains a full stop? thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String function
You can use strstr: if (strstr ($str, '.')) echo 'Full stop'; else echo 'No full stop'; Marco -- php|architect - The magazine for PHP Professionals The first monthly worldwide magazine dedicated to PHP programmers On Thu, 2002-11-28 at 08:49, Shaun wrote: Hi, Please can someone tell me how i can tell if a string contains a full stop? thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php