Help me write this function?

Hi All.

I’m looking to write a function to save me a lot of time.

I have a very large text file that I need to be formatted into html and thought I could use php to do it for me without having to go the full file. I will need to do this alot so having a funtion hand would be great.

For example look at this paragraph:

When he heard the car, the man stood up. He brushed pine needles off the seat of his jeans, then hurried out of the forest and trotted down to the roadside. As he neared the moonlit pavement, headlights swept around a corner to the south. They were very low and close together. _Could be a Jaguar._ _Has to be_, he thought.

I want it to look like this:

When he heard the car, the man stood up. He brushed pine needles off the seat of his jeans, then hurried out of the forest and trotted down to the roadside. As he neared the moonlit pavement, headlights swept around a corner to the south. They were very low and close together. <i>Could be a Jaguar.</i> <i>Has to be</i>, he thought.

I don’t need to worry about the line breaks incidentally.

I think I need some sort of str_replace() function with a rule to add “” or “” depending on the ‘replace count’ being odd or even.

I’ve had a look around but can’t find anything that is pointing me in the right direction.

Any help would be appreciated!

Hi there,

Try this:
[php]echo preg_filter("/(([^]+)_)/","$2",$string);
[/php]

THANKs! Works great. Does what I was looking for.

The only issue is that I get an error if there are any quote (" or ')marks in the $string. Anyway to get round this issue without having to lose the quotes?

I’ve tried the code with both " and ’ and it works fine. However, if you want to suppress the errors that might occur, use the @ symbol. Use it in Smokey’s code as follows

[php]echo @preg_filter("/(([^]+)_)/","$2",$string);[/php]

Use this, but I advise you… If there are other errors with the function, they will not show themselves and you might never know the exact error.

Brilliant thanks. Exactly what I needed!

Sponsor our Newsletter | Privacy Policy | Terms of Service