Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Arrow problem with a simple regular expression in php

    hello,
    I have this example code :

    $text='Business business? business. business? business, abusinessman business woman <b>business</b> [b ]business[/ b] business.';

    $new=preg_replace('/business/i', 'biziness', $text);

    echo $new.'<br>';

    This replaces every occurrence of business with biziness.

    However, I don't want it to replace "abusinessman", <b>business</b> and [b ]business[/ b].

    I don't want to replace the word business if it followed or preceded by another alphanumeric character or < > or [ ]

    I want to replace it if it is followed or preceded by spaces, comma, full stop, quotation or quote, new line, tab etc.


    How to do this?
    I am new to php regular expressions.

    Thank you very much for reading and replying!

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I'd use a "word boundary" assertion, e.g.
    PHP Code:
    $regex'/\b(business)\b/i';
    $newpreg_replace($regex'biziness'$text); 
    PCRE

  • #3
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bdl View Post
    I'd use a "word boundary" assertion, e.g.
    PHP Code:
    $regex'/\b(business)\b/i';
    $newpreg_replace($regex'biziness'$text); 
    PCRE

    hello,
    Thank you for your reply.
    I have tried your code and it converted every 'business' to 'biziness' except 'abusinessman'.
    So, it also replaced <b>business</b> and [ b ]business[ /b]

    So, I think you have missed something in the code.
    Please check again.

    Thank you.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Then specially for this case you could use the following. I have added a lookbehind negative assertion (?<! ).
    PHP Code:
    <?php 
    $text
    ='Business business? business. business? business, abusinessman business woman <b>business</b> [b ]business[/ b] business.';

    $new=preg_replace('/(?<!>|\])\bbusiness\b/i''biziness'$text);

    echo 
    $new.'<br>';
    ?>
    Please notice: for more general cases you would need more complicated regular expressions. This regexp has been suggested only for the string you have shown. If you need something more general, please ask questions.
    Last edited by SKDevelopment; 02-19-2010 at 07:37 AM.

  • Users who have thanked SKDevelopment for this post:

    student (02-19-2010)

  • #5
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Arrow

    Quote Originally Posted by SKDevelopment View Post
    Then specially for this case you could use the following. I have added a lookbehind negative assertion (?<! ).
    PHP Code:
    <?php 
    $text
    ='Business business? business. business? business, abusinessman business woman <b>business</b> [b ]business[/ b] business.';

    $new=preg_replace('/(?<!>|\])\bbusiness\b/i''biziness'$text);

    echo 
    $new.'<br>';
    ?>
    Please notice: for more general cases you would need more complicated regular expressions. This regexp has been suggested only for the string you have shown. If you need something more general, please ask questions.
    Thanks a lot.
    It seem to work.
    I will reply if I find any problem with the code.

  • #6
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Hi,
    I just now discovered that the code you gave above is converting the following the cases :

    <b>online business</b> or [b ]online business[ /b]
    or
    <b>business online</b> or [b ]business online[ /b]

    I don't want the code to modify text in the above cases.

    Please check and suggest the correct code.

    Thank you

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    As I said it was for the string you have shown only. The new regexp I am giving now uses also lookahead negative assertion (?! ) to take into account more cases you have listed. But still again, it is only for the particular cases met in the new string:
    PHP Code:
    <?php 
    $text
    ='Business business? business. business? business, abusinessman business woman <b>business</b> [b ]business[/ b] <b>online business</b> or [b ]online business[ /b] or <b>business online</b> or [b ]business online[ /b] business.';

    $new=preg_replace('/(?<!>|\])\bbusiness\b(?!<|\[)/i''biziness'$text);

    echo 
    $new.'<br>';
    ?>
    For more general case you could need more complicated regexp. So please ask questions if you could possibly need anything more complicated.

    For reference: You could also read about the assertions used in the Manual here.
    Last edited by SKDevelopment; 02-19-2010 at 08:35 AM.

  • #8
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Try

    PHP Code:
    $new=preg_replace('/(^|\W)?business(\W|$)/i''$1biziness$2'$text); 
    which is basically

    start of data or non word-character(optional)
    business
    end of data or non word-character
    ignore case

    making the bit before the word optional is necessary for where the patterns would overlap e.g. "business business" where the space would be part of the first pattern and so not eligible as part of sedcond patern (there may e another way to achieve this, but...

    If you find some of the things you want as non-word don't match \W you can replace with a character class e.g. [\W\-_] and if \W includes something you don't want as a word boundary then roll your own e.g. [\s<\[\.,:;]

    Regards,

    Dai

  • #9
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    2 DaiWelsh:
    Quote Originally Posted by DaiWelsh View Post
    PHP Code:
    $new=preg_replace('/(^|\W)?business(\W|$)/i''$1biziness$2'$text); 
    No, this would replace "business" anywhere except "abusinessman". Sorry, I am not providing a modification to your solution...

    2 Student: solution in post #7 has been checked by me and works fine.

  • #10
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Apologies, you are right, I did not read the original request properly and did what seemed logical, my bad

  • #11
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    It seems to me the request is very odd now I re-read - my thought is that the next thing will be that they don't want it to replace <b>any business online</b> as that seems to be the logical extension (unless this is an arbitrary classroom exercise ofc

    Anyway your solution is correct for the problem as currently stated, awaiting next installment..

  • #12
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    2 DaiWelsh: Yes, you are right. For <b>any business online</b> and other similar cases we would need to provide a more complicated regexp of course. I hope the OP would tell us if this could be necessary ... I think it could be (this is why I am asking for more questions in every post). You are clearly an experienced programmer. And of course you see this is the future development of the problem.

    I thought it could be possibly some learning exercise (which is fine for forums of course). So I answered without taking into account not listed cases which could be more complicated ... Just to avoid any possible confusion... Simple patterns are simpler to learn and understand ...

    Yes, of course you are right. We would have to wait for an answer from the OP before providing more help I am afraid ...

    2 Student: I am sorry for this post which could possibly be a small off-topic in your thread. If there are more cases which need to be taken into account, could you ask questions please ?

  • #13
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Hello bdl, SKDevelopment and DaiWelsh,
    Thank you for your replies.

    According to my present requirement, the code presented by SKDevelopment worked well.

    However, I think I may also need to avoid converting the 'business' inside tags like this:

    <b>any business online</b>
    or
    [ b]any business online [ /b]

    Can you please suggest modified code ?

    Thanks a lot.

  • #14
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    lol, was that a deliberate troll? If not I think you need to describe what your real objective is rather than drip-feeding test cases like this as you are wasting everyone's time.

    Regards,

    Dai

  • #15
    Regular Coder student's Avatar
    Join Date
    Jan 2007
    Posts
    129
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaiWelsh View Post
    lol, was that a deliberate troll? If not I think you need to describe what your real objective is rather than drip-feeding test cases like this as you are wasting everyone's time.

    Regards,

    Dai
    Hello Dai,
    I am extremely sorry for wasting your time.

    Here is the purpose :
    I have a phpbb3 forum.

    I am trying to convert every occurrence of some keywords in the posts into links.

    I have a business forum.
    So, whenever the word 'business' is used in the post, I would like to link it to one of my website link.

    So, it may be good to avoid replacing the text inside bbcode.

    Let me know if you would like to know more information.

    Since I am writing the code for the function myself, I could not foresee it's requirements.

    When I read your reply, I thought that I may need to implement your suggestion.

    So, I replied again requesting modification in the code.

    Thank you.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •