2014-07-21 2 views
0

이 코드에 대한 도움이 필요하며 PHP가 좋지 않습니다. 내가 projectAttr(0)에 의해 얻을 수,PHP가 특정 값을 가진 배열을 분해합니다

function projectAttr($number){ 
    global $clerk, $project; 

    $projectid = $project['id']; 
    $tags = array(); 
    $cleanUrls= (bool) $clerk->getSetting("clean_urls", 1); 

    $getTags = $clerk->query_select ("projects_to_tags", "DISTINCT tag", "WHERE projectid='$projectid' ORDER BY id ASC"); 
    while ($tag= $clerk->query_fetchArray($getTags)) 
     { 
      $tagset = explode('; ', $tag['tag']); 
     } 

    return html_entity_decode($tagset[$number]); 
} 

이 코드는 문자열을 폭발하고 배열에 넣습니다 : 여기

는 코드입니다. 그러나 나는 그 문자열에서 얻고 자하는 것에 좀 더 구체적으로 말하고 싶습니다. 나는 그것이 가치 large 등을 반환해야 projectAttr(size)를 작성하는 경우

size='large'; caption='short text about post/project'; bgcolor='black'; color='white'; 

내가 원하는 것은,이다 :

이 내 문자열입니다.

그게 가능합니까?

감사합니다, 피터

답변

0

내가 원하는 폭발에 구체적으로 답변 해 드리겠습니다보십시오. 당신은보고 당신의 projectAttr 기능을 수정해야이이 :

function projectAttr($name) { 
    global $clerk, $project; 

    $projectid = $project['id']; 
    $tags = array(); 
    $cleanUrls = (bool) $clerk->getSetting("clean_urls", 1); 

    $getTags = $clerk->query_select("projects_to_tags", "DISTINCT tag", "WHERE projectid='$projectid' ORDER BY id ASC"); 
    while ($tag = $clerk->query_fetchArray($getTags)) { 
     $items = explode('; ', $string); 

     $attr = array(); 
     foreach ($items as $item) { 
      list($key, $val) = explode('=', $item); 
      $attr[$key] = str_replace(array("'", ""), '', $val); 
     } 
    } 

    return html_entity_decode($attr[$name]); 
} 
:

Array (
    [size] => large 
    [caption] => short text about post/project 
    [bgcolor] => black 
    [color] => white 
) 

EXAMPLE DEMO


그래서 같이하는 함수를 수정 반환

$items = explode('; ', $string); 

$attr = array(); 
foreach($items as $item) { 
    list($key, $val) = explode('=', $item); 
    $attr[$key] = str_replace(array("'", ""), '',$val); 
} 

그리고 당신이 다음과 같이 호출 할 수 있도록해야합니다

projectAttr('size'); 
+0

당신에게 너무 감사합니다,이 마법처럼 일 : – user2349357

+0

완벽하게 괜찮 user2349357 @, 다행이 당신을 도와! – Darren

0

function projectAttr($number){ 
    $str= "size='large'; caption='short text about post/project'; bgcolor='black'; color='white';"; 
    $r = explode('; ', $str); 
    foreach($r as $k=>$v) { 
     $attr = explode('=', $v); 
     $projectAttr[$attr[0]] = str_replace("'", "", $attr[1]); 
    } 
    return html_entity_decode($projectAttr[$number]); 
} 
echo projectAttr('size'); // large