SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Locate string within string using wildcard

    Hi,

    How can I locate a string within a string using a sort of wildcard?

    Say i have the following string

    "Pellentesque consectetuer __VAR__ ac mauris venenatis vestibulum. __VAR1__ ut lacus.".

    How could I search the string for the __xxxx__ parts?

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regular expressions are often used for such things, although in this simple example you could just use some of php's string functions.
    PHP Code:
    $subject 'Pellentesque consectetuer __VAR__ ac mauris venenatis vestibulum. __VAR1__ ut lacus.';
    if (
    preg_match('#Pellentesque consectetuer (.+) ac mauris venenatis vestibulum. (.+) ut lacus.#'$subject$matches)) {
        
    print_r($matches);


  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, i should have explained more,

    The __xxx__s are going to vary, I just need to be able to locate them and replace them.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If they're unique, str_replace()

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could also have some fun with the preg_replace_callback function here.

    Say the text is __NAME__ or __AGE__ and it refers to $yourArray['name'] and $yourArray['age']:
    PHP Code:
    <?php
    $yourArray 
    = array('name' => 'dave''age' 32);
    function 
    getString($matches){
        global 
    $yourArray;
        
    $match $matches[1];
        
    $lmatch strtolower($match);
        return 
    array_key_exists($lmatch$yourArray) ? $yourArray[$lmatch] : ('__' $match '__');
    }
    $yourString "Hey, I'm __AGE__ and my name is __NAME__. Nice name, don't you think?";
    $yourString preg_replace_callback('/__([A-Z\d]+)__/''getString'$yourString);
    echo 
    $yourString;
    You can then add as many array items and text keys as you want.
    This can save you from having to manually search for your vars.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    @arkinstall;
    Your suggestion may come in handy when I need to replace certain strings with values from my database.

    I have my script doing what I want it to do though.

    Thanks for your help guys


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
  •