SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to trim string down to content between <!-- MARKER --> and <!-- / MARKER -->

    Let's assume that one had a string variable something like:

    $content = "garbage garbage garbage garbage <!-- MARKER --> gold gold gold gold <!-- / MARKER --> garbage garbage garbage garbage";


    What would be the best way to be able to create a variable called $gold that contains only the "gold gold gold gold" content as marked by the opening and closing of the <!-- MARKER --> tag?

    I'm having issues getting this small filtering task accomplished, any help would be much appreciated.

    Thanks.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Use a regular expression

    preg_match("/<!-- MARKER -->(.*?)<!--\ / MARKER -->/", $content, $matches);
    $gold = $matches[1];
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This demo function should do what you want.

    The output is a string of the content between the markers.


Bookmarks

Posting Permissions

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