1,105,556 Community Members

Another str replace question

Member Avatar
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi!

I'm simply need to convert a string like:

/blablabla/123123

to

123123

it should be easy I'm sure, but I can't get it :(

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

You can use explode() function: http://php.net/manual/en/function.explode.php
bye :)

Member Avatar
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Mmmm dunno if explode is the best option, the reason is that
I have this string: /blablabla/123123 thousands of times but the unique things that are always the same are the two "//", blablabla and 123123 are changing always in value and also length, I "simply" need to replace the content between the slashes with "" (I mean delete this content) and delete also both slashes

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

If you need two segments, use explode():

<?php
$a = '/blablabla/123123';
$b = explode('/',$a);
echo $b[2];
?>

If you need a single string then use str_replace():

<?php
$a = '/blablabla/123123';
$b = str_replace('/','',$a);
echo $b;
?>

;)

Member Avatar
diafol
Where are my eyes?
13,004 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,850 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
3
 
$str = "//abdhbw/1234/acljk/5678/qacjk";
echo preg_replace("/[\D]+)/","",$str);

You don't state whether you want the values into an array or whether you just want one long string of numbers. The code gives long number.

If you're certain you don't have any values like /0/, you can use this to put them into an array:

$str = "//abdhBw/1234/acljk/5678/qacjk";
$r = array_filter(preg_split("/[\D]+/",$str));
print_r($r);

But just in case, you can do this:

$str = "//abdhBw/1234/acl0jk/5678/qacjk";
preg_match_all("/[\d]+/",$str,$matches);
print_r($matches[0]);
Member Avatar
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@ardav PERFECT! Thank you!

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Oh, now I understand what you where asking for.. :D

Member Avatar
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@cereal Is more than probable that was my fault and my english, I'm doing my best, but its hard when you talk about a thing that's not yours and do it in a language that also its not yours... I master of mess ;)

Thank you any way! I luv this site and its members!

Member Avatar
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everybody again, The revenge is here! ;)

Bug found in the provided solution: but its not your fault ardav it mine.

Well the problem is that sometimes the "blablabla" section can contain also numbers, so the correct question is, how to delete inside a string like;

/blablabl234blabla/2342343453

The conten between the slashes?

Massive thanks! ;)

Member Avatar
diafol
Where are my eyes?
13,004 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,850 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
1
 

Maybe cut on '/' and check the contents for integer type.

$t = "/aldscb1234wder/q1274/12643yujo/243724/";
		$partz = explode("/",$t);
		foreach($partz as $part){
			if(preg_match("/^\d+$/",$part)){
			 $new_r[] = $part;	
			}
		}
		
		print_r($new_r);

or even:

$t = "/aldscb1234wder/123//q1274/12643yujo/243724/";
preg_match_all("/\/\d+\//",$t,$matches);
$r = str_replace("/","",$matches[0]);
print_r($r);

however, this won't be positive for start of string or end of string numbers unless there are / at start and end.

EDIT:

OK fixed

$t = "123/aldscb1234wder///1274/12643yujo/243724/q23";
		
preg_match_all("/(\b|\/)\d+(\b|\/)/",$t,$matches);
$r = str_replace("/","",$matches[0]);
print_r($r);

THat will return integers at the start and end of your string.

Member Avatar
bipies
Light Poster
35 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Perfect Again!!! I've added this to store the result in a variable:

$t = "/blablabl342/11846290496/";
		
preg_match_all("/(\b|\/)\d+(\b|\/)/",$t,$matches);
$r = str_replace("/","",$matches[0]);
print_r($r);(debug)
$result= $r[0];
echo $result; (result as I needed to play with.... ;))

T H A N K S ! ! ! !

Member Avatar
diafol
Where are my eyes?
13,004 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,850 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

OK, mark as solved with the link below the edit box.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: