search and replace not working

Discussion in 'PHP' started by Luke Jones, Jul 18, 2007.

  1. #1
    Hello,
    Could you please tell me why the following string replacement code is not working:
    function _striptext($document)
    {
    $search = array("'/pub\-.*$/;'",);
    $replace = array( "7185536717731935",);

    $text = preg_replace($search,$replace,$document);


    return $text;
    }

    Thanks for your time.
     
    Luke Jones, Jul 18, 2007 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    can you provide an example of what the value of document might be like? that might determine whether or not you need to use the values as arrays.
     
    lbalance, Jul 18, 2007 IP
  3. Luke Jones

    Luke Jones Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,
    This is a script that was written by some else. $document was only used by the programmer when he wanted to strip something out of the output. Here are his notes:
    Input: $document document to strip.
    Always used for stripping strings out of the output.
    That's all the information I can give you.
     
    Luke Jones, Jul 18, 2007 IP
  4. DavidAusman

    DavidAusman Well-Known Member

    Messages:
    399
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Not sure what are you really looking for, but the script below seems to do what you want.
    
    function _striptext($document) {
    	$replacement = "your replacement";
    	preg_match("/([pub-]+)\s*([a-zA-Z0-9]+)\s*[;]+/i", $document, $match);
    	return $match[1].$replacement;
    }
    echo  _striptext("pub-534sdf3423 ;");
    
    PHP:
     
    DavidAusman, Jul 18, 2007 IP
  5. Luke Jones

    Luke Jones Peon

    Messages:
    427
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks.
    For some reason nothing will work. There must be something in the script cancelling it out.
    Thanks anyway.
     
    Luke Jones, Jul 19, 2007 IP