str_replace and my very little brain

Hello,

I’ve recently been awarded the responsibility of building a website in wordpress. Since wordpress is fairly simple to use I’ve managed to not screw things up too badly, however I’ve run into a connundrum.

I’ve got a function that will call data from a field and display it -
so it’s [php]<?php if ( $data = get_profile_field_data( 'field=Fieldname ' ) ) : ?>[/php] to check if a field has a value, and [php]<?php member_profile_data( 'field=Fieldname' ); ?>[/php] to display the content of that field.

What I want to do is check if that field has “http://” at the beginning and strip it before it outputs, so that I can hard code the http:// in when I use the output as part of a link. This way users filling in a field called Website, for example, can enter either http://website.com or website.com, and the output will behave the same.

I have read some tutorials, but because I’m working backwards from the problem to the solution and my fundamentals exist only in the minds of those who equate one technical proficiency with being proficient in all things, I’m having some trouble making it work.

I would appreciate a quick insight into how I might go about this, and I promise that as it appears php will be in my future, I will not only learn what the heck I’m doing, I’ll try to help and contribute along the way.

Please, and thank you very much for any help.

Mike

See if this is something you can use

[php]<?php
function checkStringStartsWith ($string, $startsWith = array(‘http://’)) {
foreach ($startsWith as $s) {
if (strpos($string, $s) === 0) {
return true;
}
}
return false;
}

if (checkStringStartsWith(‘http://www.google.com’)) {
echo ‘starts with http://
’;
}

if (checkStringStartsWith(‘https://www.google.com’, array(‘https://’))) {
echo ‘starts with https://
’;
}

if (checkStringStartsWith(‘https://www.google.com’, array(‘http://’, ‘https://’))) {
echo ‘starts with either http:// or https://
’;
}[/php]

It’s generally a good idea to read the post before answering.

There is one problem with what you were suggesting (removing http:// and then adding it again later). What if I write https://www.mysite.com? Your algoritm would end up with http://https://www.mysite.com

You could do something like this

[php]<?php
function checkStringStartsWith ($string, $startsWith = array(‘http://’)) {
foreach ($startsWith as $s) {
if (strpos($string, $s) === 0) {
return true;
}
}
return false;
}

$url = ‘https://www.google.com’;

if (!checkStringStartsWith($url, array(‘http://’, ‘https://’))) {
$url = ‘http://’ . $url;
}

echo 'Fixed: ’ . $url;[/php]

Or just make a function doing this spesific task
[php]<?php
function formatUrl ($string) {
if (strpos($string, ‘http://’) !== 0 && strpos($string, ‘https://’) !== 0) {
$string = ‘http://’ . $string;
}
return $string;
}

$userInput = ‘www.google.com’;
$validUrl = formatUrl($userInput);

echo 'Fixed: ’ . $validUrl;[/php]

Thanks for the help and information so far. I’m thinking I see the logic there with your suggestion. It’s probably imbecilic of me to ask, but I assume that I load the function in my handy dandy functions file, when I call the function [php]<?php checkStringStartsWith ?> [/php] on the page, I’m not sure how to properly format the call to look at my field? Would I need to define $string = ? on the page? or just something like

[php] $url = get_profile_field_data( 'field=Fieldname ’ )

if (!checkStringStartsWith($url, array(‘http://’, ‘https://’))) {
$url = ‘http://’ . $url;
}

echo 'Fixed: ’ . $url; [/php]

Thanks again. I’ve got form validation working pretty well, but the desire is to not require formatting on entry if possible or I’d leave it be until I know more about it.

I don’t think secure URLs is going to be an issue, which was why I wasn’t considering it to begin with, but you may be correct in that some users will use them to spite my ambivalence :slight_smile:

Jim L - Thanks again. Worked it out, learned a thing or two in the process, and I’m hooked. Thanks for the assist!

Sponsor our Newsletter | Privacy Policy | Terms of Service