473,503 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do my inputs contain an underscore after an ARRAY?

30 New Member
I have the function below to create a tiny url but what happens is that when i run it through I get the out comes with a undescore(_) all except the last output.

Example:
I input

http://sample1.com
http://sample2.com
http://sample3.com
http://sample4.com

I get

http://link.mysite.org/?1234
http://link.mysite.org/?4234
http://link.mysite.org/?3566
http://link.mysite.org/?6756

but when i check them I get linked to

http://link.mysite.org/?1234 => http://sample1.com_
http://link.mysite.org/?4234 => http://sample2.com_
http://link.mysite.org/?3566 => http://sample3.com_
http://link.mysite.org/?6756 => http://sample4.com

Notice that the last one doesn't have the underscore. So im thinking its the array causing problem.

[PHP]<?php
function tinyurl($url){
// Shortens a url
$html = file_get_contents("http://link.mysite.org/create.php?url=".$url);
preg_match('/http:\/\/link\.mysite\.org\/(.*)<\/b>/', $html, $matches);
return "http://link.mysite.org/".$matches[1];
}

$url = $_POST['multi_urls'];
$lines = explode("\n", $url);
foreach ($lines as $value){
echo tinyurl($value).'<br>';
}

?> [/PHP]

Please help.
Nov 8 '07 #1
5 1833
nothing1
30 New Member
I never found out why it does what it does but i found a work-around. Check below.

[PHP]<?php

function tinyurl($url){
// Shortens a url
$html = file_get_contents("http://link.mysite.org/create.php?url=".$url);
preg_match('/http:\/\/link\.mysite\.org\/(.*)<\/b>/', $html, $matches);
return "http://link.mysite.org/".$matches[1];
}

$url = $_POST['multi_urls'];
$lines = explode("\n", $url);
$number = count($lines)-2;
for($i=0; $i<=$number; $i++){
$pro = substr($lines[$i], 0, -1);
echo tinyurl($pro).'<br>';
}
$last = count($lines)-1;
echo tinyurl($lines[$last]).'<br>';
?>[/PHP]
Nov 8 '07 #2
Atli
5,058 Recognized Expert Expert
Hi.

Considering that a URL will rarely have a underscore at either end, you may want to try the trim() function on the $value of your foreach loop.
Expand|Select|Wrap|Line Numbers
  1. echo tinyurl(trim($value, "_")).'<br>';
  2.  
Nov 8 '07 #3
nothing1
30 New Member
I was going to try trim but looking at the manual it made it seem like it will remove all the '_' in a URL which makes : http://somesite.com/which_one.php useless. I know it states only from beginning and end of string but then this example doesn't really make sense.

[PHP]
$hello = "Hello World";

$trimmed = trim($hello, "Hdle");
var_dump($trimmed);

//output: string(5) "o Wor"
[/PHP]

Thanx for the reply BTW
Nov 8 '07 #4
Atli
5,058 Recognized Expert Expert
The example makes perfect sense. Take a closer look and you will see it to.

It strips all the characters listed of the beginning/end one by one, until the character at the beginning/end is not one of the characters listed.

Consider this example, and then take another look at the example you posted:
Expand|Select|Wrap|Line Numbers
  1. $text = "1a2text1a2";
  2. echo trim($text, "12a");
  3. # outputs: text
  4.  
As to your URL problem, putting an URL through the trim function with a "_" as the second parameter will not remove underscores unless they are at the beginning or the end of the URL. (I even tested it to make sure ;)
Nov 9 '07 #5
nothing1
30 New Member
Well now I know how to use TRIM properly and you explained it pretty well. Everything is running smoothly. Thanx Again.
Nov 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4879
by: Dave Smithz | last post by:
Hi there. Because of the lack of a Union query in MySQL 3 I have decided to take the approach where I populate two arrays with values from similar tables in DB. In this case they are `courses`...
8
2486
by: kings_oz | last post by:
I have an array or items each item in the array can contain another array of other items and that array of other items can contain more array of item. I want recursively iterate through the array...
5
1902
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
1
1396
by: John Dann | last post by:
Is it possible in VB.net to have arrays of structures (ie UDTs) where the structure definition includes one or more arrays? Something like: Structure MyUDT ItemOne () as byte 'for example sub...
10
2993
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
2492
by: Geoff Berrow | last post by:
Does anyone know what regular expression I would use to split text into an array, assuming the text can be separated by any non alphanumeric character? e.g $string ="cat,dog fish, mouse ...
1
1187
by: gator6688 | last post by:
I wrote this program and so far so good but now I have to be able to count how many 1's, 2's, 3's, and 4's were entered. I am at a complete loss of where to begin. Could someone help me get started?...
2
1682
by: Rick Giuly | last post by:
Hello All, Case 1 This generates an error, which makes sense because the argument should be a list of numbers: numpy.array(10,10) Case 2 This does not generate an error and the result is an...
2
1229
by: rezanew | last post by:
Hello, I am developing an application for windows that is going to upload images to a free image hostings... I am using WebClient and UploadData for posting my form to the website... now lets...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.