Kevin Kevin - 1 year ago 80
PHP Question

Remove certain part of Posted value with php

Hello everyone im trying to retrieve data from a form with a POST request.
This data is posted into another website.
On the website where the data is created i have a text field called website. The data filled in this field goes to the other website where the data is collected. Now i want to exclude the 'www' part. for example if the user enters i want to receive only.

What i tried:

function website () {
$str = $_POST['billing_myfield12'];
echo chop($str,"www");
// end remove www

// prepare the sales payload
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".website(), <----- here i call it

This is not working. Is there a way to do this?

Answer Source

You can use trim() or specifically ltrim() to trim way the www. on the left side. Please don't forget the . after www.

echo ltrim($str, "www.");

Sample Code

echo ltrim("", "www."); //


Looks like there are side effects with the above code. Let's go with the traditional str_replace method:

echo str_replace("www.", "", $str);

Also, we are sure that it should replace only from the first characters. So, we need to use a preg_replace instead, making it replace from the start.

echo preg_replace("/^www\./g", "", $str);

Verified the above code with:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download