PHP Preg_Match Extract Comma Seperated Values from String [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP preg_match_all: Extract comma seperated list
I have a string that looks like: [something ="1,2,3"]
I need get everything between the quotes into a string. I'm sure that preg_match is the way to do this but i'm not sure of the expression to use.
$content = [something = "1,2,3"];
$new_string = preg_match('?regex?','$content');

Try this:
$content = '[something = "1,2,3"]';
if (preg_match('/"([^"]+)"/', $content, $matches)) {
$matchedContent = $matches[1];
}

Related

Laravel: Regex to strip part of a url [duplicate]

This question already has answers here:
extract part of a path with filename with php
(2 answers)
Closed 2 years ago.
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736"
My intention is to have the videos path (stripping the escaped forward slash) and the identifier set to a variable, like videos/585903773a9_654641765275736
Is this possible using str_replace or is this strictly regex required?
Try this Way. I think it will help you. I hope you will get your desire output.
<?php
// Your code here!
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736";
//remove back slashes
$str1 = stripslashes($string);
//count total string length.
$len = strlen($str1);
//pick the position of /videos
$pos = strpos($str1,'/vid');
//split it from the url.
$break_string = substr($str1, $pos, $len);
//output: /videos/585903773a9_654641765275736
echo($break_string);
?>

How to add - in-between strings [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);

php - How to remove html tag using regular expression? [duplicate]

This question already has answers here:
Stripping html tags using php
(7 answers)
PHP removing html tags from string
(6 answers)
Closed 4 years ago.
value of $attributes['st_title'] is Physical Design Engineers
I want to remove and . The final expected output is: $attributes['st_title'] = Physical Design Engineers
I am trying as follows:
$pattern[0] = "/<a.[^>]+>/";
$pattern[1] = '/</a>/';
$replacement[1] = '';
$replacement[0] = '';
$attributes['st_title'] = preg_replace( $pattern, $replacement, $attributes['st_title'] );
But it sets $attributes['st_title'] as empty. Any idea?
Since you want text only from the link, so use strip_tags()
echo strip_tags($text);
https://eval.in/979039
Use php strip_tags()
or
preg_replace("/<.*?>/", "", $attributes['st_title']);

How to get specific substring digit from string using php? [duplicate]

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
$fileName = "Backup_1486874341.tar.gz"
Here is my file name. How can i get only timestamp from that file name Like 1486874341
Use simple regex in preg_match() to select target digit in file name.
$fileName = "Backup_1486874341.tar.gz";
preg_match("/_(\d+)./", $fileName, $matches);
echo $matches[1];
See result in demo
Here is the sample snippet:
<?php
$str = 'Backup_1486874341.tar.gz';
preg_match_all('!\d+!', $str, $matches);
print_r($matches[0][0]);
?>

How to Remove a specific character from the end of the string in php or regex [duplicate]

This question already has answers here:
Removing the last character of a string IF it is $variable [duplicate]
(5 answers)
Closed 12 months ago.
I am generating a string dynamically. For example The string looks like this
$string = "this-is-a-test-string-for-example-";
It can also be like this
$string = "this-is-a-test-string-for-example";
I want if there is a hyphen "-" at the end of the string, It should be removed. How can I do that in php or regex?
http://pl1.php.net/trim - that function gets characters to trim as last parameter
trim($string,"-");
or as suggested for right side only
rtrim($string,"-");
If it always at the end (right) of the string this will work
$string = rtrim($string,"-");
$cleanedString = preg_replace('/^(this-is-a-test-string-for-example)-$/', '$1', $string);
$delete = array('-');
if(in_array($string[(strlen($string)-1)], $delete))
$string = substr($string, 0, strlen($string)-1);
You can add other characters to delete into $delete array.

Categories