#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Germany
    Posts
    228
    Rep Power
    12

    Finding and replacing one meta tag


    Hi all,

    I am trying to replace the keyword meta tag:

    Code:
    <meta name="keywords" content="my keywords, x, y, z" />
    with some other keywords. I am using preg_replace in PHP, but can't seem to get it to work. :/

    This is how my code currently looks:

    PHP Code:
    preg_replace('/<meta name=\"keywords\" content=\"(.*)\" \/>/i','<meta name="keywords" name="my new keywords" />'$htmlOutput); 
    can someone help me here please?

    thanks and kind regards

    Sven
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    .* is greedy so will match everything up to the last '" />' sequence in your string, not the next one. A quick fix would be to use .*? instead. Better yet is to avoid dot-star altogether. Though Perl-focused, this article is worth a read.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    Germany
    Posts
    228
    Rep Power
    12
    Thanks for the link. This helps getting more insight in regular expressions, which I am really not good at.

    After trying around for an hour I decided to post this problem here. A few minutes later I find the error in a misspelled variable. The regular expression was working all the way...

    Sry for posting!

IMN logo majestic logo threadwatch logo seochat tools logo