Jump to content

After 4 days! I need help!


jamesxg1

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/223863-after-4-days-i-need-help/
Share on other sites

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.

..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);

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.

..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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.