Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I really need some help with this code and I'm not that good at PHP.

Here is the code:

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]);
}

The code explodes a string and puts it in a array, that I can get by projectAttr(0). But I want to be more specific in what I want to get from the string.

This is my string:

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

What I want is, if I write projectAttr(size) it should return the value large and so forth.

Is that even possible?

Thanks, Peter

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I'll answer specifically to the explode you want. You should modify your projectAttr function to look this this:

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

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

Which returns

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

EXAMPLE DEMO


So modify your function to look like this:

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]);
}

And that should allow you to call it like this:

projectAttr('size');
share|improve this answer
    
Thank you so much, this one worked like a charm :) –  user2349357 Jul 21 '14 at 7:42
    
@user2349357 Perfectly alright, glad it helped you out! –  Darren Jul 21 '14 at 7:44

try

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.