jamesxg1 Posted January 9, 2011 Share Posted January 9, 2011 Hiya peeps! I have been trying to get this working for 4 days now! My brain has now gone into overload and I need some help! I need to replace the content closed in curly brackets in $replacements. $replacements = '<a href="blah.php?id={id}">{name}</a><br />'; $data = array(); $data[] = array('id' => 1, 'name' => 'james'); $data[] = array('id' => 2, 'name' => 'test'); // this data will be fetched from a database using mysql_fetch_array. I need the output to be; <a href="blah.php?id=1">james</a><br /> <a href="blah.php?id=2">test</a><br /> Can anyone help? Any help will be greatly appreciated! Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/ Share on other sites More sharing options...
Anti-Moronic Posted January 9, 2011 Share Posted January 9, 2011 Can you not just construct the link from scratch or do you have to use replace? I mean, you can use str_replace to do this quite easily: $customLink = str_replace('{id}', $data['0']['id'], $replacements); $customLink = str_replace('{name}', $data['0']['name'], $customLink); EDIT: let me just redo that loop. Quote Link to comment https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/#findComment-1157021 Share on other sites More sharing options...
Anti-Moronic Posted January 9, 2011 Share Posted January 9, 2011 ..not sure if there is a cleaner way to do this, but this is quite flexible: $replace = array('id','name'); foreach($data as $key => $user){ $temp[] = $replacements; foreach($replace as $match){ $temp[] = str_replace('{'.$match.'}', $data[$key][$match], end($temp)); } $data[$key]['customLink'] = end($temp); unset($temp); } var_dump($data), outputs: array 0 => array 'id' => int 1 'name' => string 'james' (length=5) 'customLink' => string '<a href="blah.php?id=1">james</a><br />' (length=39) 1 => array 'id' => int 2 'name' => string 'test' (length=4) 'customLink' => string '<a href="blah.php?id=2">test</a><br />' (length=38) You can, of course, store the links in their own array and echo: $customLinks[] = end($temp); echo implode($customLinks); Quote Link to comment https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/#findComment-1157026 Share on other sites More sharing options...
jamesxg1 Posted January 9, 2011 Author Share Posted January 9, 2011 Can you not just construct the link from scratch or do you have to use replace? I mean, you can use str_replace to do this quite easily: $customLink = str_replace('{id}', $data['0']['id'], $replacements); $customLink = str_replace('{name}', $data['0']['name'], $customLink); EDIT: let me just redo that loop. Hiya Anti-Moronic, Thank you for a prompt reply! I originally was going to do this, the issue I have is the content closed in curly brackets will be different everytime, infact the whole $replacements string will be. The content closed in curly brackets will be the name of the row I need there so if its {id} I need have something like $row[$content] ($content being id and $row being the data fetch). The string $replacements was only an example (sample data) to get the script working in my test script as I dont have any sample mysql data so I have to create the sample data in text rather than in mysql. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/#findComment-1157083 Share on other sites More sharing options...
jamesxg1 Posted January 9, 2011 Author Share Posted January 9, 2011 ..not sure if there is a cleaner way to do this, but this is quite flexible: $replace = array('id','name'); foreach($data as $key => $user){ $temp[] = $replacements; foreach($replace as $match){ $temp[] = str_replace('{'.$match.'}', $data[$key][$match], end($temp)); } $data[$key]['customLink'] = end($temp); unset($temp); } var_dump($data), outputs: array 0 => array 'id' => int 1 'name' => string 'james' (length=5) 'customLink' => string '<a href="blah.php?id=1">james</a><br />' (length=39) 1 => array 'id' => int 2 'name' => string 'test' (length=4) 'customLink' => string '<a href="blah.php?id=2">test</a><br />' (length=38) You can, of course, store the links in their own array and echo: $customLinks[] = end($temp); echo implode($customLinks); This worked! I actually cannot believe I spent 4 days on this! Thank you so much! I am in your debt! LOL! Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/#findComment-1157089 Share on other sites More sharing options...
Anti-Moronic Posted January 11, 2011 Share Posted January 11, 2011 Hey! Forgot to check up on this. Glad it worked out! Quote Link to comment https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/#findComment-1157958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.