Jump to content


str_replace help


3 replies to this topic

#1 BlazeForc3

    Young Padawan

  • Members
  • Pip
  • 62 posts
  • Location:Australia

Posted 30 June 2006 - 09:05 PM

Hey does anyone know how I can get str_replace to just replace the first occurrence of a string instead of all occurrences?

Edit: Ive found a solution to my original problem but now i have another question, im using the php gd library to add text to a png image, I am able to get the text onto the image easy enough and in the right position but the text is written from left to right, its sorta hard to explain, i still want the text to be read from left to right but i want it to be written from right to left the same way if u were to right align the text in a word document.

Edited by BlazeForc3, 30 June 2006 - 10:39 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 01 July 2006 - 01:24 AM

The only solution i've ever found to that, is to count the characters, do some math, and make a best guess for the position of the first character. I never got it to work 100% for some reason, but it basically went like:
i's, l's, !'s, took up one pixel.
Everything else took up 4.
There was 2 pixels of space between the characters, with 4 pixels for a space.

Count the i's, l's, and !'s, multiply by 1, count remaing characters, multiply by 4, add together... blah blah blah... Got a semi-right aligned text, but it wasn't perfect (give or take 8px's on each line).

#3 BlazeForc3

    Young Padawan

  • Members
  • Pip
  • 62 posts
  • Location:Australia

Posted 02 July 2006 - 01:15 PM

View Postrc69, on Jul 1 2006, 06:24 AM, said:

The only solution i've ever found to that, is to count the characters, do some math, and make a best guess for the position of the first character. I never got it to work 100% for some reason, but it basically went like:
i's, l's, !'s, took up one pixel.
Everything else took up 4.
There was 2 pixels of space between the characters, with 4 pixels for a space.

Count the i's, l's, and !'s, multiply by 1, count remaing characters, multiply by 4, add together... blah blah blah... Got a semi-right aligned text, but it wasn't perfect (give or take 8px's on each line).

yeh thats basically what i did, i made an example using the maxium string which was 88888 and i used some functions in php to determine the maximum string length in pixels, then i figured out the length of the string 8888 and figured out the difference in string length and the difference in the position of the string and i was able to compensate... yeh very confusing

this is what i used to figure out the string width and string x position...

$string_width = imagefontwidth($font1)*strlen($text2);
$string_x_position = ($image_width-$string_width)/2;

so i knew the string width and x position of the maximum string and then compared it to a smaller string and found that for every 1 pixel difference in string width there was a 0.5 difference in x position, i noticed u said ur method wouldnt work 100% of the time, mine seems too i hope this helps you out

Edited by BlazeForc3, 02 July 2006 - 01:48 PM.


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 04 July 2006 - 02:34 AM

As i've long since abandoned the project, it won't help me. But it will definately give me and future readers a helping hand the next time i try to attempt it.

The use of imagefontwidth is an interesting idea as well, i've never heard of that function (which makes you special, as i've already claimed to know all of them :D)

Edited by rc69, 04 July 2006 - 02:34 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users