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

Link to comment
Share on other sites

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

Link to comment
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.

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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