I am trying to take the Jira case details / comments and use the details in our support / utility system and want to replace {code} and {noformat} tags with <code></code>
tags so that the formatting is similar to what is shown in Jira
I have tried a few regex expressions with no luck so far.
Here is what I have tried so far
/({code}(?:(?!{code}).)*){code}((?:(?!{code}).)*)/
to replace every other {code} tage with </code>
and /{code}/
to then replace the remaining {code} tags with <code>
This also does not take into account the chance that a {code} tag may have {noformat} tags inside and these should not be replaced or vice versa.
Here is the same code I am working with
$jira = new JiraClient\JiraClient('https://jira.url', 'myUser', 'myPass');
try {
$issue = $jira->issue()->get($ticketJira);
//$caseBody = $issue->getDescription()
//For testing Let's Hardset the caseBody
$caseBody = 'Example Code: {code}{ "fields": { "project": { "key": "JIRA" }, "summary": "Build Jira API into Support Interface to help raise cases.", "description": "Creating of an issue using project\\
keys and issue type names using the REST API\\
{code}Markup Test{code}", "issuetype": { "name": "Production Task/Issue" }, "customfield_10800": [{"value":"Not applicable"}], "fixVersions": [{"name":"3.0.136.2"},], "versions": [{"name":"3.0.136.2"},] } }{code}';
// Replace {code} / noformat tags here
$descRaw = $issue->getDescription();
$despPre = preg_replace('/({code}+)/', '<code>', $descRaw,1);
$desc = preg_replace('/(.*(?:(?!{code}).)*){code}((?:(?!.*).)*)/', '\\1</code>\\2', $despPre);
echo('<label class="control-label">'.$issue->getKey().' - '.$issue->getSummary().'</label>');
echo("<p class='modal-content' style='text-align: left !important;'>".nl2br($desc)."</p>");
echo('');
}
catch (\JiraClient\Exception\JiraException $e) {
echo('<p>'.$e.'></p>');
}
The 2 issues I am having are, the inline tags are also being converted and that I would also want this to work with {noformat} tags instead of having to do a second pass.
This one should do what you want: {code}(.*?){code}
. Associated to preg_replace()
, it will replace all {code}
occurences by <code>
(or </code>
if a previous one is found).
An example which also replaces {noformat}
by <code>
:
$str = '{code}var a = 1;{code} then, {noformat}a = 5;{noformat} and, {code}a -= 4;{code}';
$codePatterns = [
'/{code}(.*?){code}/',
'/{noformat}(.*?){noformat}/'
];
// output: <code>var a = 1;</code> then, <code>a = 5;</code> and, <code>a -= 4;</code>
echo preg_replace($codePatterns, '<code>$1</code>', $str);
Explanation of the regular expression:
{code}
matches literally {code}
(.*?)
...()
captures the content between for later use.
matches any character, except for new line*?
matches between zero and unlimited times the previous character, as few times as possible{code}
matches literally {code}
Note: If I used .*
instead of .*?
, the output would be:
<code>var a = 1;{code} then, <code>a = 5;</code> and, {code}a -= 4;</code>
I guess that's why you ended with a such complicated expression.