SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: $myNumber plus

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,211
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    $myNumber plus

    Code:
    $myNumber=100;
    
    
    $myVar='myVar is plus5 which is '.$myNumber.'+5
    myVar is plus10 which is '.$myNumber.'+10
    myVar is plus20 which is '.$myNumber.'+20';
    I have a number variable "$myNumber" which is like the above.


    I like to replace the text plus5 to 105, the text plus10 to 110 and the text plus20 to 120.

    So my target result will be like the following.
    Code:
    target result
    
    $myTarget_result='myVar is 105 which is 100+5
    'myVar is 110 which is 100+10
    myVar is 120 which is 100+20';
    How can I get $myTarget_result from $myVar?

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,825
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Code:
    $myNumber=100;
    
    
    $myVar='myVar is plus5 which is '.$myNumber.'+5
    myVar is plus10 which is '.$myNumber.'+10
    myVar is plus20 which is '.$myNumber.'+20';
    I have a number variable "$myNumber" which is like the above.


    I like to replace the text plus5 to 105, the text plus10 to 110 and the text plus20 to 120.

    So my target result will be like the following.
    Code:
    target result
    
    $myTarget_result='myVar is 105 which is 100+5
    'myVar is 110 which is 100+10
    myVar is 120 which is 100+20';
    How can I get $myTarget_result from $myVar?
    Using str_replace?
    PHP Code:
    $myTarget_result str_replace(array('plus5''plus10''plus20'), array($myNumber 5$myNumber 10$myNumber+20), $myTarget_result); 
    This is assuming you weren't wanting to concatenate the string (like you did with $myNumber)
    PHP Code:
    $myVar='myVar is '.($myNumber+5).' which is '.$myNumber.'+5
    myVar is '
    .($myNumber+10).' which is '.$myNumber.'+10
    myVar is '
    .($myNumber+20).' which is '.$myNumber.'+20'
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


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
  •