This question already has answers here:
Remove all html tags from php string
(9 answers)
Regex str_replace
(2 answers)
Closed 5 years ago.
sorry for not beeing able to put the title exactly what my question, I can't find a way to ask it.
So im trying to replace all ' in a text inside and replace it for ' in php
I have a variable $desc = "he's such a good person and he'll be very successfull";
and I'm trying to do the following
$desc = str_replace("'","'",$desc);
But no success is there a way to use regex in str_replace?
Yes it looks to be fine now... for some reason.
Is there a way to use regex for it?
for example to remove html tags from the text?
$desc = "<strong> he's such a good person </strong> <br /> he'll be very successfull";
$desc = str_replace("<*>"," ",$desc);
You may try to use the correct PHP function to do this job, so please take a look at : preg_replace doc.
In your case, you would like to use like this :
preg_replace('/'/', "'", $desc);
Take a look at this execution:
https://eval.in/800948
Does it have to be a regex because this is a lot simpler way to do it
$desc = "he's such a good person and he'll be very successfull";
$new = str_replace(''', "'", $desc);
echo $new;
RESULT:
he's such a good person and he'll be very successfull
You don't need to use regex as it will be more complex
use
$desc = "he's such a good person and he'll be very successfull";
$desc = str_replace("'","'",$desc);
echo "after replace :"." ".$desc;
it is more simpler :)
Related
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
The following is automatically output via editor:
1001-web-file
I would like to add an ID:
<a id="replace" href="https://someurl.com/1001-web-file.pdf">1001-web-file</a>
Then use str_replace and a regular expression to find the anchors in question and replace with:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
What I've managed to do is:
$replace = array(
'<a id="pdfthumb" href="' => '<img src="',
'.pdf">pdfthumb</a>' => '-pdf-290x300.jpg"/></br>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
This works to tear down the anchor tag and rebuild as an img. But I can't do much more. I played around with some regex to create a wildcard and realized I need to create a variable for the href to use when I rebuild the HTML, but I'm stuck.
Any insight is much appreciated :)
In your case, I think it should be easier if you do it on client side using javascript.
My background is not PHP but you can use the same pattern to test with your PHP code:
Input:
1001-web-file
Output:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
var input = '1001-web-file';
var pattern = /href=\"(.+)\.pdf\"/;
var match = input.match(pattern)[1];
input = input.replace(/(<a.*>).*(<\/a>)/, '$1<img src="' + match + '-pdf-290x300.jpg">$2');
console.log(input)
Here is translated php code of Tan javascript answer
$input = '1001-web-file';
preg_match('/href="(.+)\.pdf"/', $input, $m);
$match = $m[1];
$input = preg_replace('/(<a.*>).*(<\/a>)/', '$1<img src="'. $match .'-pdf-290x300.jpg">$2', $input);
echo $input;
This question already has answers here:
how to replace hyphen with blank space / white space? php
(2 answers)
Closed 8 years ago.
So, I have a piece of code I am using in most of my pages which sets the page title as the file name. This is because I don't want to have to change the title each time I create a new page.
This all works fine, except I don't want to have spaces (or rather, %20) being shown in the url when the file name contains spaces. I'd much rather have hyphens in place of the spaces, as it looks cleaner to the user. However, this means PHP will set the page title also with hyphens instead of spaces, which, frankly, looks ugly.
Is there a way I can rewrite this code to replace the hyphens with spaces?
The code:
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst($path_parts['filename']);
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use str_replace to replace the - with a space.
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst(str_replace('-', ' ', $path_parts['filename']));
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use implode adn explode for this
$string = "the string";
$arr = explode("string to replace",$string);
$string = implode("string to add ",$arr);
echo $string;
This question already has an answer here:
XPath - select text after certain node
(1 answer)
Closed 9 years ago.
I have following string
<strong>Test: </strong> BD-F5300
I am interested in getting number BD-F5300. Number could be of any thing text,number.
Any help, how can I get it? Thanks.
You could make use of preg_replace
<?php
$str='<strong>Test: </strong> BD-F5300';
echo $str = preg_replace("~<(/)?strong>(.*?)<(/)?strong>~","", $str);
OUTPUT :
BD-F5300
do like this in JavaScript:
var src = "<strong>Test: </strong> BD-F5300";
var reg = /.*<\/.*>\s*([a-zA-Z0-9-]+)/g;
var group = reg.exec(src);
console.log(group[1]+'\r\n'); //group[1] is what you want !
If all you need is to get some content after </strong> then you can just use:
preg_match('#</strong> (.+)#', $string, $matches);
The desired match will be in $matches[1]. However, this requires that the <strong> tag and the text content you want to find are both on the same line. If there are multiples of these you want to match, you may want to use preg_match_all
If there is always a space before the beginning of the final text you want and if there are never any spaces in the actual number text you want, you can avoid regex by doing this:
$str = '<strong>Test: </strong> BD-F5300';
$solution = substr($str, strrpos($str, ' ') + 1);
var_dump($solution);
This question already has answers here:
Batch script to replace PHP short open tags with <?php
(13 answers)
Closed 9 years ago.
I need to replace all PHP lines that are like <?=[something]?> with this: <?php echo [something]; ?> The problem is that there can be almost anything in that something clause even ? and < but they can't be adjacent. I am kinda new to regexes and wrote this very messy expression: \<\?\=([a-zA-Z0-9()=#<>\[\]\\/'"._$\?:, \-]*)([;]*)\?\> and replaced it with <?php echo \1; ?>.
It works but it can't match something like this:
<?=[something]?><tr><td><?=[something]?> when it's all in one line. It matches the whole line altogether.
Any help will be greatly appreciated.
This code should work:
$s = '<?=[something]?><tr><td><?=[something]?>';
$s = preg_replace('/(<\?)=\s*(\[[^]]*\])\s*(\?>)/', '$1php echo $2; $3', $s);
var_dump($s);
OUTPUT:
string(56) "<?php echo [something]; ?><tr><td><?php echo [something]; ?>"
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
replace any url's within a string of text, to clickable links with php
Just a quick question, When I post links like http://www.buddyweb.me it will just appear like that but, but t's not automaticly linked. So how can I replace the http://www.buddyweb.me with Google
Any suggestions are apreciated, thanks :)
Jut like here
function clickable($url){
$url = str_replace("\\r","\r",$url);
$url = str_replace("\\n","\n<BR>",$url);
$url = str_replace("\\n\\r","\n\r",$url);
$in=array(
'`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
'`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
);
$out=array(
'<a href="$1" rel=nofollow>$1</a> ',
'<a href="http://$1" rel=\'nofollow\'>$1</a>'
);
return preg_replace($in,$out,$url);
}
$replaced = preg_replace('/(http[s]?:\/\/[^\s]*)/i', '$1', $url);
No need for preg-replace, just concatenate varibles around your link.
<?
$yourlink = "http://www.buddyweb.me";
$yourDescriptor = "Google";
$linkedlink = "$yourDescriptor";
echo $linkedlink;
?>
echo preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?#\[\]+]*)(/[\w\#$%&~/.\-;:=,?#\[\]+]*)?)#is", "\\1\\2", $string);
I would consider this a complicated Regular Expression. However, if you're interested in learning more, I really liked getting started with this video http://www.youtube.com/watch?v=DRR9fOXkfRE
Call something that will return it how you like.
<?php
$link = "http://stackoverflow.com";
$name = "Stack Overflow";
echo href($link, $name);
function href($link, $name){
$link = "$name";
return $link;
}
?>