Jump to content

Counting characters in a string and replacing it


Go to solution Solved by Ch0cu3r,

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'])); ?>
  • Solution

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);
}
Edited by Ch0cu3r

 

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;
}

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.