Filter URL with regex in PHP

Is someone willing to help me filtering a URL using regex? I've come pretty far, but I stumble upon one last issue.
Scenario:
- The user submits the link of their SoundCloud song in a Gravity Forms website field.
- The script automatically ads https:// automatically if the user does not add it.
- Removes www. and m. from the URL.
- Sometimes a link with a private URL extension is submitted: https://soundcloud.com/username/songtitle/s-qciX1vDI2Cq
What can I do so the script also removes s-qciX1vDI2Cq from the URL too?
Example input http://www.soundcloud.com/username/songtitle/s-qciX1vDI2Cq
Example output https://soundcloud.com/username/songtitle
Many thanks in advance!
add_filter( 'gform_pre_render', 'itsg_check_website_field_value' );
add_filter( 'gform_pre_validation', 'itsg_check_website_field_value' );
function itsg_check_website_field_value( $form ) {
foreach ( $form['fields'] as &$field ) { // for all form fields
if ( 'website' == $field['type'] || ( isset( $field['inputType'] ) && 'website' == $field['inputType']) ) { // select the fields that are 'website' type
$value = RGFormsModel::get_field_value($field); // get the value of the field
if (! empty($value) ) { // if value not empty
$field_id = $field['id']; // get the field id
if (! preg_match("~^(?:f|ht)tps?://~i", $value) ) { // if value does not start with ftp:// http:// or https://
$value = "https://" . $value; // add https:// to start of value
}
if ( preg_match("/(https?:\/\/)(www\.|m\.)?soundcloud\.com\/([^\s\n]+)\/([^\s\n]+)\/([^\s\n]+)", $value)) {
$temp = explode("/", $value);
array_pop($temp);
$value = implode("/", $temp);
}
preg_match("/(https?:\/\/)(www\.|m\.)?([^\s\n]+)(\/+)?/", $value, $extractedDomain);
$value = "https://" . $extractedDomain[3];
preg_match('/^(.*?)(\?.*)?$/', $value, $noSearch);
$value = trim($noSearch[1], '/') . '';
$_POST['input_' . $field_id] = $value; // update post with new value
}
}
}
return $form;
}
Answer
Solution:
Use regex pattern
^(?:https?:\/\/|)(?:www|m)\.(soundcloud\.com\/[^\/]+\/[^\/]+)(?:\/.*?|)$
and do a replacement with
http://$1
Test it and see explanation (on right top corner) at https://regex101.com/r/mwa4JP/1
See PHP demo at https://www.ideone.com/rdKb3P
preg_replace("/^(?:https?:\/\/|)(?:www|m)\.(soundcloud\.com\/[^\/]+\/[^\/]+)(?:\/.*?|)$/",
"http://$1", $input);
To accept uppercase letters in optionalwww.
andm.
prefixes and/or in thesoundcloud.com
domain name, addi
regex modifier:
/^(?:https?:\/\/|)(?:www|m)\.(soundcloud\.com\/[^\/]+\/[^\/]+)(?:\/.*?|)$/i
Answer
Solution:
I would go for the regex '///'
$url = 'http://www.soundcloud.com/username/songtitle/s-qciX1vDI2Cq';
$regex = '/\//';
$a = preg_split($regex, $url);
print_r($a);
output:
Array
(
[0] => http:
[1] =>
[2] => www.soundcloud.com
[3] => username
[4] => songtitle
[5] => s-qciX1vDI2Cq
)
Now you can join these elements from 0 to 4 to give the correct url.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the requested url was not found on this server. xampp
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.