1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Rep Power

    Preg_replace.. confusion

    ok I have an xml that gets generated automatically:

    <namedattr name='Gender'>4</namedattr>
    <namedattr name='Profile'>5</namedattr>
    <namedattr name='voucher_code'>10</namedattr>

    then I do a str_replace to replace numbers with data and for some reason in voucher code, i get voucher and value for profile?

    PHP Code:
    $xml_copy = $xml;
            krsort ( $lead );
            $search =  array_keys ( $lead ) ;
            $replace = array_values ( $lead );
            echo "\n";
            print_r ( $search );
            echo "\n";
            print_r ( $replace );
            echo "\n";
            $xml_copy = str_replace($search, $replace, $xml_copy );
            $xml_copy = '<?xml version="1.0" ?><member xmlns="http://webservices.ecircle-ag.com/ecm">' . preg_replace("{>(\d)<\/}", "></", $xml_copy );
    printing search and replace gives me:

    PHP Code:
    0] => 10
    [1] => 5
    [2] => 3
    [3] => 2
    [4] => 1

    PHP Code:
    0] => vouchercode
    [1] => profile
    [2] => eee
    [3] => ddd
    [4] => test@email.com

    The xml ends up looking like:

    PHP Code:
    <?xml version="1.0" ?>
    <member xmlns="http://webservices.ecircle-ag.com/ecm">
    <namedattr name='Gender'></namedattr>
    <namedattr name='Profile'>Profile</namedattr>
    <namedattr name='voucher_code'>VoucherProfile</namedattr></member>

    As an aside question:

    Can someone please help me with this (i.e. a better way)

    WHat i am doing is, i have stored for each "company" the attribues needed into database, when someone posts through my api. I create the xml based on company attribute (like above) and then get the data sent and insert (using str_replace ) into my xml.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Rep Power

    wherever you got this output from, it's not from the code you gave us. Your alleged input XML ends with a free-floating </user>, but the output XML ends with a </member> instead. According to your code, "5" is replaced with "profile". But your output contains "Profile" instead.

    This thing is very, very weird. What's the whole purpose of those strange identifier numbers?

    Fumbling with string functions to process XML is also a no-no. The whole purpose of XML is that it's structured. It has a specific structure and meaning. You treat it like a random pile of characters, completely disregarding any context. For example, what if you had an element named address2? Then your string functions would mangle that element and spit out

    That's obviously wrong.

    Anyway, I first need to know the idea behind this whole approach.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Rep Power
    1. </user> was my fault, should have been [/code]..
    2. come on, a type of Profile and profile shouldnt get you into a twist...

    The whole idea behind is:

    We have different companies who post data to us, which I send to my database. it is a direct api sort of.. what is meant to happen is you do:


    and this url then stores details into a local db.

    The cron job runs a script (where the problem occurs) which:

    1. looks at different clients stored in db
    2. creates an xml on the fly for each client (which is what I did above, but with numbers instead of %s, because its not efficient for me to create same xml again and again for same client and insert data into point 3 below.
    3. uses the xml to insert data ( in a foreach loop )
    4. send each data to the external database.

    Now they can send data directly to external database, but for this particular client, I need to add in extra parameter which I need to get from my local database.

IMN logo majestic logo threadwatch logo seochat tools logo