Jump to content

Removing extra words and taking right word


heeha
 Share

Recommended Posts

Hi, I am new to php and still learning. I have been not able to work for few  months due to work but Now again trying to learn. 

 

I have a php form : 

<form method="post" action="test.php">
Name=<input type="text" name="yourname">
submit<input type="submit">
</form>

I have a file called test1.php. It has many names 

 

Lets say :  Ron, ronda, carl, mathew, alexa,tina, lima, soniya, viktoria. List is big but lets count to this much. 

 

Now i want that if a user puts a name form as "iviktoriana" . I want to remove this extra"i" and  "na" words after form processing. I don't want to remove it while processing the form. 

 

How do i use make it work?Do i need to put these names in array in test1.php 

 

Link to comment
Share on other sites

Wut?

 

So you have a list of names (for whatever reason), a user enters their name, and now you want to map the chosen name to a vaguely similar predefined name. According to which rules? Does "alex" map to "alexa"? Does "tixxxna" map to "tina"?

 

And why on earth are you doing this? Is it just a random exercise you've come up with?

Link to comment
Share on other sites

Wut?

 

So you have a list of names (for whatever reason), a user enters their name, and now you want to map the chosen name to a vaguely similar predefined name. According to which rules? Does "alex" map to "alexa"? Does "tixxxna" map to "tina"?

 

And why on earth are you doing this? Is it just a random exercise you've come up with?

No, may be I wasn't able to put it the way I want to. 

Let me try it again. 

When user enters a name say "Metina", and "tina" name is already in the list. So I want to make that form still get processed but I want to show the user that your name contains word "tina" in it. We don't allow tina words therefore, we already have that word. Like, a user has "mygoogle" in his website name and i want to place a tag, that your name has "google" and we don't allow you to use that because google is others property. 

Link to comment
Share on other sites

When you want help, post your real problem. Don't make up some bullshit fantasy example, because this is almost guaranteed to fail miserably.

 

So what you actually want to do is check if the input contains a string from an array. The strpos() function searches a single substring within a single string. If you combine that with a loop, you can search for multiple words and print the matches.

Link to comment
Share on other sites

When you want help, post your real problem. Don't make up some bullshit fantasy example, because this is almost guaranteed to fail miserably.

 

So what you actually want to do is check if the input contains a string from an array. The strpos() function searches a single substring within a single string. If you combine that with a loop, you can search for multiple words and print the matches.

I have a website developed by other. In that users can check SEO data and some of them are people who has domain name carrying words of trademark companies. Like some people has mygoogle.com. So the form always gets processed and shows SEO data like Domain authority, social shares and alike. But I want to show users another message that your domain name also contains trademark names in it and you shouldn't use such domain names.  As i am not a developer so i don't know how to achieve that. I don't want my developer to do it because i want to learn myself and show the information. All i was trying to do is that I want to create a another file carrying the list of Trademark companies and when users process a domain name that has trademark names inside them, I want to print that message for them along with the SEO data processed. 

Link to comment
Share on other sites

OK, now we at least know what the task is.

 

My suggestion in post #4 will solve this problem. If you don't now how to implement it, show us what you've tried and explain exactly what you don't understand.

 

In any case, you do need basic PHP and programming knowledge for us to have to discussion. If you don't know how to create an array or write a loop, this isn't the right place. You should start with Codeacademy or buy a book.

Edited by Jacques1
Link to comment
Share on other sites

OK, now we at least now what the task is.

 

My suggestion in post #4 will solve this problem. If you don't now how to implement it, show us what you've tried and explain exactly what you don't understand.

 

In any case, you do need basic PHP and programming knowledge for us to have to discussion. If you don't know how to create an array or write a loop, this isn't the right place. You should start with Codeacademy or buy a book.

Thanks for suggestion. I am learning php from w3schools and I will take a look at codeacademy also. I know basics about for and foreach and strings, and few others. I will try to implement what you said in post 4 and will see if it works. 

Link to comment
Share on other sites

 

You don't even need a loop. You can check the whole array with a single statement

if ($input != str_replace($myArray,'', $input) ) {
    echo "Contains an existing value";
}

This actually worked like a charm : 

 

<html>
<body>
<form method="get" action="test1.php">
Name <input type="name" name="url">
<input type="submit">
</form>
<?php 
$url = $_GET['url'];
echo $url;
$a= array("google", "bing", "alexa");


if($url != str_replace($a,'',$url)){
echo "$url, you have violated this trademark.";
} else {
echo "result faild";
}
?>

</body>
</html>

I tried another way: 

<html>
<body>
<form method="get" action="test1.php">
Name <input type="name" name="url">
<input type="submit">
</form>
<?php 
$url = $_GET['url'];
echo $url;

if(strpos($url, 'google') !==false){
echo "this is working";
} else {
echo "result failed";
}
//this one also works but it actually takes one "value" not many."

?>

</body>
</html>
Link to comment
Share on other sites

Yeah, and now let's think for a second and decide which one is more useful:

  • Barand: “Dear user, there's some trademark in the name you've entered, but I won't tell you which one. Good luck finding it!”
  • Me: “Dear user, the name you've entered contains the term ‘Google’ which is a registered trademark of Google Inc. Please choose a different name.

 

//this one also works but it actually takes one "value" not many."

 

That's why I told you to use strpos() together with a loop. As in: Loop over the words, and if the name contains the word, tell the user:

<?php

// you can store additional information like the trademark owner if you use an *associative* array with the trademarks
// as the key and the information as the value
$trademarks = [
    'google',
    'bing',
    'alexa',
];

$found_trademarks = [];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // collect all trademarks found within the submitted name
    foreach ($trademarks as $trademark)
    {
        if (strpos($_POST['url'], $trademark) !== false)
        {
            $found_trademarks[] = $trademark;
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
    <form method="post">
        <label>URL: <input type="text" name="url"></label>
        <input type="submit">
    </form>
    <?php if ($found_trademarks): ?>
        <p>Warning: The URL you have entered contains the following trademarks. We strongly recommend you chose a different URL.</p>
        <ul>
            <?php foreach ($found_trademarks as $trademark): ?>
                <li><?= htmlspecialchars($trademark, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</body>
</html>
Edited by Jacques1
Link to comment
Share on other sites

 

Yeah, and now let's think for a second and decide which one is more useful:

  • Barand: “Dear user, there's some trademark in the name you've entered, but I won't tell you which one. Good luck finding it!”
  • Me: “Dear user, the name you've entered contains the term ‘Google’ which is a registered trademark of Google Inc. Please choose a different name.

 

 

That's why I told you to use strpos() together with a loop. As in: Loop over the words, and if the name contains the word, tell the user:

<?php

// you can store additional information like the trademark owner if you use an *associative* array with the trademarks
// as the key and the information as the value
$trademarks = [
    'google',
    'bing',
    'alexa',
];

$found_trademarks = [];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // collect all trademarks found within the submitted name
    foreach ($trademarks as $trademark)
    {
        if (strpos($_POST['url'], $trademark) !== false)
        {
            $found_trademarks[] = $trademark;
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
    <form method="post">
        <label>URL: <input type="text" name="url"></label>
        <input type="submit">
    </form>
    <?php if ($found_trademarks): ?>
        <p>Warning: The URL you have entered contains the following trademarks. We strongly recommend you chose a different URL.</p>
        <ul>
            <?php foreach ($found_trademarks as $trademark): ?>
                <li><?= htmlspecialchars($trademark, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</body>
</html>

Thanks, I would surely see your example. This is good for learning. 

But Actually i needed what Barand said, I told that I want to show the information after the form has been processed. It mean's users already have the domain name with trademark names in them. They were there to check web data. During that time I wanted to show that information. Your's is good if i want to invite people to register a domain name, which I am not. 

Link to comment
Share on other sites

Since the names are plain data, it doesn't make a lot of sense to use an external script. A JSON file or any other data format works just as well and is a lot safer: If you screw up the external script while editing the names, the whole application is dead. But if you screw up the JSON file, you can go on without the feature.

 

 

trademarks.json

[
    "bing",
    "google",
    "alexa",
    "microsoft"
]
<?php

$trademarks = json_decode(file_get_contents(__DIR__.'/trademarks.json'), true);

var_dump($trademarks);
Edited by Jacques1
Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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