SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unable to interchange characters of a string using PHP

    Hi all

    I have a taken a tree input as PHP string
    e.g ((APPLE MANGO)(BALL BAT))

    I have an application in which the users select a REFERENCE object. In this example if the user selects BALL as reference object, then my string must become

    ((BALL MANGO)(APPLE BAT))

    i.e the REFERENCE object and the first object must be swapped...
    Please suggest some solutions. How can i manipulate this string using PHP.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    And the structure is always like this?
    ((word1 word2)(word3 word4))

    I'd say first you'd have to get the four words in an array. With preg_match() for example:
    PHP Code:
    $words preg_match('/\(\((.*?) (.*?)\)\((.*?) (.*?)\)\)/''((APPLE MANGO)(BALL BAT))'); 
    This will give this result:
    Code:
    Array
    (
        [0] => ((APPLE MANGO)(BALL BAT))
        [1] => APPLE
        [2] => MANGO
        [3] => BALL
        [4] => BAT
    )
    So if you ignore the first element, the next four will contain the four words.
    Now all you have to do is find the reference object, and swap it with the first object, and then recreate the string.

  3. #3
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the suggestion, but my problem is regarding the reconstruction of the string. As i dont know how many objects will be there in the string and how many braces. So i am unable to insert braces between the objects

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Well, lets try and define the tree. What I see in your tree is:

    Code:
         0
       /   \
      1     2
     / \   / \
    3  4  5  6
    Where 0,1,2 are empty, 3,4,5, and 6 are the words.

    Is this true? Is this tree always going to be empty except for the leaves?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Assuming that's the structure, then preg_split the string on "( or \s", capturing the delimiter. That will deconstruct your string. Find the first non-parenthesis index, find your target index, set the target index to the value at the first index, then set the first index to your target, and implode the array over ''.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •