regular expressions 2

Discussion in 'PHP' started by roice, Feb 21, 2010.

  1. #1
    Hello,
    I have database of words and synonyms words. for example: hello -> hi, welcome, hola, hii.
    images -> pics, pictures, photos and ect...

    I'm trying to write a code that get html file from user (e.g: sample.html) and searching for the words in the database that have synonyms words. if the code found word like that it will replace it with random word from its words bank.

    for example, if the HTMl file contain "<h1>hello</h1> lets talk about <b>images</b>", the result maybe: "<h1>welcome</h1> lets talk about <b>pictures</b>".

    I start to write the code but I don't know how to search exact word and how to replace it when I find it(if i'm searching for "girl", I don't wont to replace "girlfriend", only "girl").

    can anyone help me with that?
     
    roice, Feb 21, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Ellaborate your confusing.

    Can you reply with an input and your expected output.
     
    danx10, Feb 21, 2010 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    of course,
    input: <h1>hello</h1> lets talk about <b>images</b>
    output could be: <h1>welcome</h1> lets talk about <b>pictures</b>
     
    roice, Feb 21, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    This should start you off:

    <?php
    
    $input = "<h1>hello</h1> lets talk about <b>images</b>";
    
    $term = "hello";
    
    $replacement = "welcome";
    
    $output = preg_replace("|<h1>".preg_quote($term)."</h1>|", $replacement, $input);
    
    echo $output;
    
    ?>
    PHP:
     
    danx10, Feb 21, 2010 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi danx10
    Thanks for your help.
    the code that I wrote was only for example. I don't know how the HTML file gonna look like, that's why I need it to be a general code that fit to all HTML types....
     
    roice, Feb 21, 2010 IP
  6. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    how do I search for exact word for replacement, using preg_match_all?
     
    roice, Feb 22, 2010 IP