Rewrite spaces into %20 on custom profile field

Discussion in 'PHP' started by bradenkeith, Jul 7, 2008.

  1. #1
    I've got a custom profile field for the users to put their Xbox Live gamertag into. Then on memberlist.php I made it to display their Xbox Live Gamercard instead of just the gamertag. The problem is when they have a space in their gamertag it wont render the card, it give an error. This is because Xbox Live requires them to have %20 to fill in the spaces, now what can I do to make it render as %20 instead of space?

    This is with phpBB 3(.3, I think)
     
    bradenkeith, Jul 7, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    urlencode or str_replace.

    Either does a good job of it, I would recommend urlencode.
     
    Danltn, Jul 7, 2008 IP
  3. bradenkeith

    bradenkeith Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could you show me how to write that? I'm unfamiliar with PHP for the most part and am more of a HTML/CSS fella. Thanks.
     
    bradenkeith, Jul 7, 2008 IP
  4. whirlybird20

    whirlybird20 Guest

    Messages:
    462
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    whirlybird20, Jul 7, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    If you're going to use a site. At least your www.PHP.net ;)

    $gamertag = urlencode($gamertag);

    I don't know your variable name, but there's a guess.

    Dan
     
    Danltn, Jul 7, 2008 IP
  6. bradenkeith

    bradenkeith Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    	
    	// Custom Profile Fields
    		$profile_fields = array();
    		if ($config['load_cpf_viewprofile'])
    		{
    			include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
    			$cp = new custom_profile();
    			$profile_fields = $cp->generate_profile_fields_template('grab', $user_id);
    			$profile_fields = (isset($profile_fields[$user_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields[$user_id]) : array();
    		}
    
    
    Code (markup):
    I believe this is what I need to parse it into urlencode, or should I make it's own variable somehow.... hmmm?
     
    bradenkeith, Jul 7, 2008 IP
  7. bradenkeith

    bradenkeith Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok I changed my mind, here's the code you really need:
    
    			// Go through the fields in correct order
    			foreach (array_keys($this->profile_cache) as $used_ident)
    			{
    				foreach ($field_data as $user_id => $row)
    				{
    					$user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
    					$user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
    				}
    			}
    
    			return $user_fields;
    		}
    		else if ($mode == 'show')
    		{
    			// $profile_row == $user_fields[$row['user_id']];
    			$tpl_fields = array();
    			$tpl_fields['row'] = $tpl_fields['blockrow'] = array();
    
    			foreach ($profile_row as $ident => $ident_ary)
    			{
    				$value = $this->get_profile_value($ident_ary);
    
    				if ($value === NULL)
    				{
    					continue;
    				}
    
    				$tpl_fields['row'] += array(
    					'PROFILE_' . strtoupper($ident) . '_VALUE'	=> $value,
    					'PROFILE_' . strtoupper($ident) . '_TYPE'	=> $ident_ary['data']['field_type'],
    					'PROFILE_' . strtoupper($ident) . '_NAME'	=> $ident_ary['data']['lang_name'],
    					'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],
    
    					'S_PROFILE_' . strtoupper($ident)			=> true
    				);
    
    				$tpl_fields['blockrow'][] = array(
    					'PROFILE_FIELD_VALUE'	=> $value,
    					'PROFILE_FIELD_TYPE'	=> $ident_ary['data']['field_type'],
    					'PROFILE_FIELD_NAME'	=> $ident_ary['data']['lang_name'],
    					'PROFILE_FIELD_EXPLAIN'	=> $ident_ary['data']['lang_explain'],
     
    					'S_PROFILE_' . strtoupper($ident)		=> true
    				);
    			}
    		
    			return str_replace(" ", "%20", $tpl_fields);
    
    Code (markup):
    My good try is at the very bottom that starts with return str_replace...

    Can somebody please help me :( thanks :D
     
    bradenkeith, Jul 7, 2008 IP