Jump to content

[SOLVED] Need a string function


Cep

Recommended Posts

Hello,

 

Just wondering if anyone knows which string function would allow me to search a random string for a single character so that I can replace them with HTML elements.

 

For example, this string,

$string = "it was a #great# day and tomorrow #will# be even better!";

 

I would like to string function to come up with this,

 

$string = "it was a <b>great</b> day and tomorrow <b>will</b> be even better!";

 

(I'm not going to use those tags but I am just giving an example of what I am looking for, maybe its a combination of string functions but any help would be great!)

Link to comment
Share on other sites

i would do something like, preg_replace() or str_replace(). however, first, what i would do is write a function that searches every character in the string for '#'. each time it finds one, it will incriment a counter. then, use str_replace or preg_replace for each '#' that was incrimented as an odd number. and another preg or str replace for all the '#' that were incrimented as an even number. also, you should put somewhere in your code that if the number of '#' is an odd number, then it must mean that someone didn't close the '#' tag somewhere in the string.

 

i'm not sure how you would code this, but you may be able to do it this way:

write a function to find each '#'. add them all up, if it is an even number, continue with the function, if not, return an error.

 

if it is an even number, then search the string again for each '#'. each time this loop returns true, incriment the counter, and then immediately fall into an if statement.

 

if the incriment is an odd number, preg_replace with an 'open-bold-tag'. elseif incriment is an even number, preg_replace with a 'close-bold-tag'.

 

 

in theory, i guess it works. but i have no idea how you'd search a string by each character, and how to return something if it finds a character. maybe explode the string into an array and use array_walk() to apply your function, then implode it back into a string?

 

 

 

you may also get use out of this tutorial

Link to comment
Share on other sites

Well to find the number of instances I would use

 

$hashes = substr_count($content, "#");

 

Where $content is the string. I like the idea of looping through but how do you determine what is an odd or even number? I would also use str_replace as its faster. However you also need to split the string wouldn't you?

Link to comment
Share on other sites

Well to find the number of instances I would use

 

$hashes = substr_count($content, "#");

 

Where $content is the string. I like the idea of looping through but how do you determine what is an odd or even number?

 

i didn't know about substr_count() thanks for that. anyway, in theory, you'd have to incriment it on an event basis. meaning, every time your function came across the '#', it would have to know to incriment. the only way to do this that i can come up with right now, is to explode() a string into a single character per index array. if this is possible. then you'd use foreach to apply a certain set of tasks to each index in the array. so let's say, hypothetically, you can split an entire string into an array with one character in each index. now, you'd do something like this.

<?php
        $i = 0;
        $array = explode($string);

        foreach($array as $key => $val){
                if($val == "#"){
                        if($i % 2 == 0){
                                str_replace($val, "your_open_tag");
                                $i++;
                        }else{
                                str_replace($val, "your_close_tag");
                                $i++;
                        }
                }
        }
        $string = implode($array);
?>

 

does that make any sense? i'm sure the arguments for str_replace are wrong, but you get the idea.

 

 

NOTE: i added a link at the end of my last post to a tutorial. you may get some use out of it =)

Link to comment
Share on other sites

@JBS

Ah of course! Divide is / , well that modulus is good to know thanks I'll use that in the future.

 

@boo_lolly

When you explode the string you remove the explode element as it were don't you? For example,

 


$string = "boo#didley";

$fragments = explode("#", $string);

echo $fragments[0]; // "boo"

echo $fragments[1]; // "didley"

 

This would mean I couldn't do a conditional as the char is removed during the explode? I will take a look over the tut thanks. And I like the idea you have come up with. Yes your missing the str_replace($oldstring, $newstring, $thestring);

Link to comment
Share on other sites

Thats pretty close mbtaylor :) however now try the string,

 

$string = "it #was a great# day and tomorrow #will# be even better!";

 

Very good work though by the way :) wish I was more familiar with preg_replace.

 

Link to comment
Share on other sites

Its ok figured it out, you just put a space between the A-Z a-z don't you :D

 

Many thanks for your help guys, your the best! :)

 

Just for your future reference, you could've put the space anywhere between the [ and ].

 

In regular expressions that's know as a character set, and it lets you combine sets of characters together, and (most of the time) where you place it doesn't matter.

 

And boo_lolly, there's no wrong way to skin a cat, just more efficent ways of doing so. ;D

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.