I need some php code

by 26 replies
29
Hopefully this might just be a few lines of code
and if so I would be grateful if someone could supply.
(My head is spinning with ereg pattern matching)


The requirement:

--------------------------------------------------------
I have a string which can contain any number of 'tokens'.
A token is text enclosed by {}

So for example

==> Hello {abc}, goodbye {def}

I want to remove the {} and replace each token
with a string derived from the token.

So I end up with

==> Hello xxx, goodbye yyy

where xxx = function (abc)
yyy = function (def)
--------------------------------------------------------

Thanks
Harvey
#programming #code #php
  • Hey Harvey,

    My php skills are still towards the "newbie" side.

    But... you can post this prob on tek-tips.com. It's my
    secret weapon whenever I have a coding question.
    You should get an answer within a day or so.

    Hope this helps.

    Best,
    -Jay
    • [1] reply
    • Hi Harvey

      I did put some thought into this but it's not the easiest thing to do. I guess that's why you've not had a working reply yet.

      The parsing of the original string to pull out all the tokens between the { and } symbols is the tricky bit because there's no one function that will do it.

      It would need a function written to look for each { then read to the } to build the token, process and replace it (the easy part) and continue until all tokens had been read.

      It's not that difficult, just a bit of a pain.

      But I guess you already knew all that

      Cheers,

      Neil
      • [1] reply
  • I'd just do it using the string replace function:

    $rawString = "Hello {abc}, goodbye {def}";

    $newString = str_replace("{abc}", "Harvey", $rawString);
    $newString = str_replace("{def}", "Sam", $newString);

    echo $newString;


    ....gives you:

    Hello Harvey, goodbye Sam


    Quick and easy, just the way I like my code

    cheers
    Sam
  • Banned
    Learn regular expressions.

    And then you will be able to replace patterns , such as "{XXXXXX}".
    • [1] reply
    • That's actually the easy part of the problem.
  • Banned
    Is this for a membership site login and logout???

    What I do to learn these kinda things is by studying code from the CMS programs (wordpress,joomla, etc.)

    Then I implement it.

    Hope this helps...
    • [1] reply
    • No it's not


      Well I could spend hours searching and studying ask an expert
      at the Warrior forum !

      Harvey
  • Banned
    Then what is the primary purpose...???

    I spend the hours it takes to learn the simplest things....

    I don't rely on others information...
    • [1] reply
    • Well - not that it matters - but it's an addition to
      software which I have had developed for me

      But I'm not planning to become a software developer
      and won't need to use this information again.

      The hours lost in getting this solution could
      be put to better use. Agree ?


      Harvey
  • Banned
    Agreed...

    take a look at this....

    PHP: strtok - Manual
    • [1] reply
    • Thanks - I've had a quick look but not sure if that will
      help as it would extract the text outside of the tokens as
      well as inside

      Harvey
  • I stole this from some open source code. It may give you and idea of how to proceed.

    function fill($template, $values) {

    if (sizeof($values) != 0) {
    // go through all the values
    for(reset($values); $key = key($values); next($values)) {
    $template = str_replace("{$key}",$values[$key],$template);
    }
    }

    // remove non matched template-tags
    return preg_replace('/{[a-zA-Z]+}/','',$template);
    }
    • [2] replies
    • Jim

      Thank you for that.

      I only know php basics so that's too difficult for me
      to follow but someone else reading this thread
      might be able to use it


      Harvey
    • Thanks! It helps! I think soon I'll become so good programmer!!
  • OK. Not a PHP programmer, but I can manage.
    Here is the code stolen from above.
    ===========================
    $s='Hello {abc}, goodbye {def}';

    echo preg_replace_callback('/\{([^{}]+)\}/',whatever,$s);

    function whatever($what)
    {
    return strtoupper($what[1]);
    }

    ==============================

    Wht's happening:

    preg_replace_callback = the first funny string basically says match everything in {} that is not a curly bracket that gets your ABC or DEF

    whatever is the name of a function

    $s is the string you are looking through "Hello {abc}, goodbye {def}" in the example

    So in the function "whatever" this code returns the uppercase version of the string that was matched (abc or def converts to ABC or DEF)

    so you would replace the functionality in there with something like

    $lookup = strtoupper($what[1]); //Assumes you want a case insensitive lookup
    //Use $lookup to find the value you want, such as using a map variable or looking the value up in the DB (which would be very slow if you do it repeatedly).


    ===========================
    $s='Hello {abc}, goodbye {def}';

    echo preg_replace_callback('/\{([^{}]+)\}/',replace_token_with,$s);

    function replace_token_with($what)
    {
    $lookup = strtoupper($what[1]);
    return get_value_of($lookup);
    }

    ==============================
    • [1] reply
    • Thanks Roger, though I have now adapted and implemented the code
      in my program.

      If you are interested to know what it was for it was to
      allow cloaked links in a PDF rebranding tool.

      I need to change { { url } } to decodeurlscript.php?u = url
      throughout a page of HTML.

      You can see it in action here

      Try the Demo (Item 3) at
      Login


      Harvey
  • Needs a login
    So you have wanted something like

    function replace_token_with($what)
    {

    return "http://www.example.com/decodeurlscript.php?u=" + urlencode($what[1]);
    }

    I've never found doing in place substitutions inside PDF files to work well, especially since it requires uncompressed PDFs.

    How do you do it? Just a simple replace, or modify a base (eg postscript) fiel and generate from that?
    • [1] reply
  • [DELETED]

Next Topics on Trending Feed