-8

I want to replace each occurency of font size="somenumber" (the number every time is different!) with the font-size="somenumber".

How to make this with php?

I have a text that looks like this:

Hello <font size="4">today</font> is my
<font size="3">special day</font> and am
<font size="11">ready</font> for it...

And i want to convert it into this:

Hello <span style="font-size:4;">today</span> is my 
<span style="font-size:3;">special day</span> and am
<span style="font-size:11;">ready</span> for it...
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
Evafas
  • 1
  • 1
  • 2
    The `` tag brings me great memories from when I was a young boy :_) – Álvaro González Sep 24 '14 at 10:33
  • Hello Mark am Eva and am newbie almost noob so please forgive me. With my knowlegde i tried this one : $convert= str_replace('someword', 'somwword', $petropas); ...but as you can see is not doing this i want, how can i make this work for ervey possible value (number) that may the text includes ? Maybe using regex stuff but i have not idea about regex ! – Evafas Sep 24 '14 at 10:33
  • Why do you want to replace text with php like this? Looks more like you need to alter your template. And that is usually beeing done in an IDE or some code editor. Use the text replace function with a regex. – Nico O Sep 24 '14 at 10:35
  • If you can help me with that regex cause i have no knwoledge of regex yet i will be thankful ! – Evafas Sep 24 '14 at 10:37
  • I want to do this convertion beacuse am using nicedit which is creating font size values insted of modern style="font-size: px;" – Evafas Sep 24 '14 at 10:39

2 Answers2

0

You can use preg_replace(). http://php.net/manual/en/function.preg-replace.php

$str = 'Hello <font size="4">today</font> is my <font size="3">special day</font> and am <font size="11">ready</font> for it...';
preg_replace('/<font size="(\d+)">(.+?)<\/font>/', '<span style="font-size:$1;">$2</span>', $str)

Output:

Hello <span style="font-size:4;">today</span> is my <span style="font-size:3;">special day</span> and am <span style="font-size:11;">ready</span> for it...
marian0
  • 3,336
  • 3
  • 27
  • 37
  • Thanks so much Marian !!! is working as you say but is not adding the px in font-size:4; the correct must be font-size:4px (with px) in order to work the code, can you please fix it a bit ? – Evafas Sep 24 '14 at 10:48
  • So add it to replacement string in `preg_replace()` - `'$2'` – marian0 Sep 24 '14 at 10:50
  • Thanks i finnaly found it too $2 this make the thing work ! Sorry for my noobiness.. but am new on programming :) Just one more question on it.. now a formely font size="4" will be a font-size:4px; but in reality is not the same value with font-size: 4px; the real size is much much smaller.... uhh any idea how to fix this ? – Evafas Sep 24 '14 at 10:51
  • Related: [You can't parse \[X\]HTML with regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Álvaro González Sep 24 '14 at 11:16
0

Just a little hint to get you started: you need to check the XML Manipulation chapter in the PHP manual, more specifically the Document Object Model extension:

Happy coding!


P.S. I suppose you're working with third-party HTML but you should be aware that presentation tags like <font> were replaced by something better when CSS was released in 1996.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360