Jump to content

Counting characters in a string and replacing it


therocker

Recommended Posts

Hello, how would you count the number of characters in a string and then replacing the string with astrics?

 

So some examples are if I have a list of 6 strings like this

morning, noon, afternoon, evening, night, dawn

each string gets their own astrics

 

So for morning, there's 7 characters in that string. Then replace that word with 7 astrics like so.

*******

And then the same with the rest.

 

Noon has 4 characters so replace it with 4 astrics

****

I have this as of right now which is my only code that counts characters. I would like someone to help me fix it and modify it.

<?php print(strlen($row['original_row_goes_here'])); ?>

You'd add those words to an array, then loop through them, get the words length using strlen(). Then use str_repeat() to create a string of astrics that is the length of the word to filter. Something like

foreach($filter_words as $word)
{
    $replacement = str_repeat('*', strlen($word));
    $text = str_replace($word, $replacement, $text);
}

 

You'd add those words to an array, then loop through them, get the words length using strlen(). Then use str_repeat() to create a string of astrics that is the length of the word to filter. Something like

foreach($filter_words as $word)
{
    $replacement = str_repeat('*', strlen($word));
    $text = str_replace($word, $replacement, $text);
}

Thanks this helped. Had to do a little modifing, but I got it. Thanks.

$filter_words = array('morning', 'noon', 'afternoon', 'evening', 'night', 'dawn');

foreach($filter_words as $w => $word)
{
    $replacement = str_repeat('*', strlen($word));
    $text = str_replace($word, $replacement, $word);

    echo $text;
}

Archived

This topic is now archived and is closed to further replies.

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